Can't access an object through its name as a string











up vote
0
down vote

favorite












I try to find out, how it is possible to access an object through a string which has the same name as the object name. Is there any specific notations for the string to be interpreted as the object name?



Like in my example script:






<html>
<body>
<script>
var door = {"opened":false, "window":{"opened":false} };

function knocking(event){
var knockedThing = event.target.id;

alert(knockedThing);
alert("door is opened : " + knockedThing.opened);
alert("door window is opened : " + knockedThing.window.opened);
}
</script>

<button id="door" onclick="knocking(event)">A door</button>

</body>
</html>





The script can be run here:
---> https://www.w3schools.com/code/tryit.asp?filename=FX3JE6FP2UFQ



The goal of my script is to use the id "door" of the clicked button to access the object "door".
But right now it doesn't work because the button id "door" is just a string name which have nothing to do with the object with same name.



How it is possible to make the connection without creating an object which would integrate the object "door" (to make this one accessible through the brackets notation)?



var house = {"door":{"opened":false, "window":{"opened":false} } };


Which would be accessible through bracket notation, like this:



alert(house[something]);
alert("door is opened : " + house[something].opened);
alert("door window is opened : " + house[something].window.opened);









share|improve this question




























    up vote
    0
    down vote

    favorite












    I try to find out, how it is possible to access an object through a string which has the same name as the object name. Is there any specific notations for the string to be interpreted as the object name?



    Like in my example script:






    <html>
    <body>
    <script>
    var door = {"opened":false, "window":{"opened":false} };

    function knocking(event){
    var knockedThing = event.target.id;

    alert(knockedThing);
    alert("door is opened : " + knockedThing.opened);
    alert("door window is opened : " + knockedThing.window.opened);
    }
    </script>

    <button id="door" onclick="knocking(event)">A door</button>

    </body>
    </html>





    The script can be run here:
    ---> https://www.w3schools.com/code/tryit.asp?filename=FX3JE6FP2UFQ



    The goal of my script is to use the id "door" of the clicked button to access the object "door".
    But right now it doesn't work because the button id "door" is just a string name which have nothing to do with the object with same name.



    How it is possible to make the connection without creating an object which would integrate the object "door" (to make this one accessible through the brackets notation)?



    var house = {"door":{"opened":false, "window":{"opened":false} } };


    Which would be accessible through bracket notation, like this:



    alert(house[something]);
    alert("door is opened : " + house[something].opened);
    alert("door window is opened : " + house[something].window.opened);









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I try to find out, how it is possible to access an object through a string which has the same name as the object name. Is there any specific notations for the string to be interpreted as the object name?



      Like in my example script:






      <html>
      <body>
      <script>
      var door = {"opened":false, "window":{"opened":false} };

      function knocking(event){
      var knockedThing = event.target.id;

      alert(knockedThing);
      alert("door is opened : " + knockedThing.opened);
      alert("door window is opened : " + knockedThing.window.opened);
      }
      </script>

      <button id="door" onclick="knocking(event)">A door</button>

      </body>
      </html>





      The script can be run here:
      ---> https://www.w3schools.com/code/tryit.asp?filename=FX3JE6FP2UFQ



      The goal of my script is to use the id "door" of the clicked button to access the object "door".
      But right now it doesn't work because the button id "door" is just a string name which have nothing to do with the object with same name.



      How it is possible to make the connection without creating an object which would integrate the object "door" (to make this one accessible through the brackets notation)?



      var house = {"door":{"opened":false, "window":{"opened":false} } };


      Which would be accessible through bracket notation, like this:



      alert(house[something]);
      alert("door is opened : " + house[something].opened);
      alert("door window is opened : " + house[something].window.opened);









      share|improve this question















      I try to find out, how it is possible to access an object through a string which has the same name as the object name. Is there any specific notations for the string to be interpreted as the object name?



      Like in my example script:






      <html>
      <body>
      <script>
      var door = {"opened":false, "window":{"opened":false} };

      function knocking(event){
      var knockedThing = event.target.id;

      alert(knockedThing);
      alert("door is opened : " + knockedThing.opened);
      alert("door window is opened : " + knockedThing.window.opened);
      }
      </script>

      <button id="door" onclick="knocking(event)">A door</button>

      </body>
      </html>





      The script can be run here:
      ---> https://www.w3schools.com/code/tryit.asp?filename=FX3JE6FP2UFQ



      The goal of my script is to use the id "door" of the clicked button to access the object "door".
      But right now it doesn't work because the button id "door" is just a string name which have nothing to do with the object with same name.



      How it is possible to make the connection without creating an object which would integrate the object "door" (to make this one accessible through the brackets notation)?



      var house = {"door":{"opened":false, "window":{"opened":false} } };


      Which would be accessible through bracket notation, like this:



      alert(house[something]);
      alert("door is opened : " + house[something].opened);
      alert("door window is opened : " + house[something].window.opened);





      <html>
      <body>
      <script>
      var door = {"opened":false, "window":{"opened":false} };

      function knocking(event){
      var knockedThing = event.target.id;

      alert(knockedThing);
      alert("door is opened : " + knockedThing.opened);
      alert("door window is opened : " + knockedThing.window.opened);
      }
      </script>

      <button id="door" onclick="knocking(event)">A door</button>

      </body>
      </html>





      <html>
      <body>
      <script>
      var door = {"opened":false, "window":{"opened":false} };

      function knocking(event){
      var knockedThing = event.target.id;

      alert(knockedThing);
      alert("door is opened : " + knockedThing.opened);
      alert("door window is opened : " + knockedThing.window.opened);
      }
      </script>

      <button id="door" onclick="knocking(event)">A door</button>

      </body>
      </html>






      javascript variables






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 16:05









      melpomene

      56.8k54487




      56.8k54487










      asked Nov 9 at 21:58









      mosis

      237




      237
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted













          <html>
          <body>
          <script>
          var door = {"opened":false, "window":{"opened":false} };

          function knocking(event){
          var knockedThing = event.target.id;

          alert(knockedThing);
          alert("door is opened : " + window[knockedThing].opened);
          alert("door window is opened : " + window[knockedThing].window.opened);
          }
          </script>

          <button id="door" onclick="knocking(event)">A door</button>

          </body>
          </html>





          Global variables are properties of the window object, so you can access door through window['door'].






          share|improve this answer





















          • Thanks Melpomene! I am not realy sure to understand what you mean. Isn't your solution what I am also suggesting thru my house[something] example in the second part of my message? What I would like to know is if there is a solution without to have to use the [ ] notation. But maybe it's not possible
            – mosis
            Nov 12 at 11:21












          • @mosis Why do you not want to use [ ]?
            – melpomene
            Nov 12 at 21:08










          • Hi, because I found more clear and confortable to access the object door directly by inputing door instead to have to create a parent object including my doorobject as property. And then to have to input parentObject[door] to acces it. And then I think I can't pass parentObject[door] directly as parameter in function(or can I ?)
            – mosis
            Nov 13 at 9:24










          • @mosis Why do you think you need to create a parent object? Did you not read my answer?
            – melpomene
            Nov 13 at 18:40










          • Hi melpomene, I do read your answer wich is working well. Maybe "parent object" is not the correct term to express that the variable parentObject is including door (making door a kind of child, by becoming a property). I don't know, as I am not an experienced programmer, I was thinking that working without [ ] if possible, would help to maintain my script clean.
            – mosis
            Nov 15 at 11:06













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233780%2fcant-access-an-object-through-its-name-as-a-string%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted













          <html>
          <body>
          <script>
          var door = {"opened":false, "window":{"opened":false} };

          function knocking(event){
          var knockedThing = event.target.id;

          alert(knockedThing);
          alert("door is opened : " + window[knockedThing].opened);
          alert("door window is opened : " + window[knockedThing].window.opened);
          }
          </script>

          <button id="door" onclick="knocking(event)">A door</button>

          </body>
          </html>





          Global variables are properties of the window object, so you can access door through window['door'].






          share|improve this answer





















          • Thanks Melpomene! I am not realy sure to understand what you mean. Isn't your solution what I am also suggesting thru my house[something] example in the second part of my message? What I would like to know is if there is a solution without to have to use the [ ] notation. But maybe it's not possible
            – mosis
            Nov 12 at 11:21












          • @mosis Why do you not want to use [ ]?
            – melpomene
            Nov 12 at 21:08










          • Hi, because I found more clear and confortable to access the object door directly by inputing door instead to have to create a parent object including my doorobject as property. And then to have to input parentObject[door] to acces it. And then I think I can't pass parentObject[door] directly as parameter in function(or can I ?)
            – mosis
            Nov 13 at 9:24










          • @mosis Why do you think you need to create a parent object? Did you not read my answer?
            – melpomene
            Nov 13 at 18:40










          • Hi melpomene, I do read your answer wich is working well. Maybe "parent object" is not the correct term to express that the variable parentObject is including door (making door a kind of child, by becoming a property). I don't know, as I am not an experienced programmer, I was thinking that working without [ ] if possible, would help to maintain my script clean.
            – mosis
            Nov 15 at 11:06

















          up vote
          1
          down vote



          accepted













          <html>
          <body>
          <script>
          var door = {"opened":false, "window":{"opened":false} };

          function knocking(event){
          var knockedThing = event.target.id;

          alert(knockedThing);
          alert("door is opened : " + window[knockedThing].opened);
          alert("door window is opened : " + window[knockedThing].window.opened);
          }
          </script>

          <button id="door" onclick="knocking(event)">A door</button>

          </body>
          </html>





          Global variables are properties of the window object, so you can access door through window['door'].






          share|improve this answer





















          • Thanks Melpomene! I am not realy sure to understand what you mean. Isn't your solution what I am also suggesting thru my house[something] example in the second part of my message? What I would like to know is if there is a solution without to have to use the [ ] notation. But maybe it's not possible
            – mosis
            Nov 12 at 11:21












          • @mosis Why do you not want to use [ ]?
            – melpomene
            Nov 12 at 21:08










          • Hi, because I found more clear and confortable to access the object door directly by inputing door instead to have to create a parent object including my doorobject as property. And then to have to input parentObject[door] to acces it. And then I think I can't pass parentObject[door] directly as parameter in function(or can I ?)
            – mosis
            Nov 13 at 9:24










          • @mosis Why do you think you need to create a parent object? Did you not read my answer?
            – melpomene
            Nov 13 at 18:40










          • Hi melpomene, I do read your answer wich is working well. Maybe "parent object" is not the correct term to express that the variable parentObject is including door (making door a kind of child, by becoming a property). I don't know, as I am not an experienced programmer, I was thinking that working without [ ] if possible, would help to maintain my script clean.
            – mosis
            Nov 15 at 11:06















          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted









          <html>
          <body>
          <script>
          var door = {"opened":false, "window":{"opened":false} };

          function knocking(event){
          var knockedThing = event.target.id;

          alert(knockedThing);
          alert("door is opened : " + window[knockedThing].opened);
          alert("door window is opened : " + window[knockedThing].window.opened);
          }
          </script>

          <button id="door" onclick="knocking(event)">A door</button>

          </body>
          </html>





          Global variables are properties of the window object, so you can access door through window['door'].






          share|improve this answer















          <html>
          <body>
          <script>
          var door = {"opened":false, "window":{"opened":false} };

          function knocking(event){
          var knockedThing = event.target.id;

          alert(knockedThing);
          alert("door is opened : " + window[knockedThing].opened);
          alert("door window is opened : " + window[knockedThing].window.opened);
          }
          </script>

          <button id="door" onclick="knocking(event)">A door</button>

          </body>
          </html>





          Global variables are properties of the window object, so you can access door through window['door'].






          <html>
          <body>
          <script>
          var door = {"opened":false, "window":{"opened":false} };

          function knocking(event){
          var knockedThing = event.target.id;

          alert(knockedThing);
          alert("door is opened : " + window[knockedThing].opened);
          alert("door window is opened : " + window[knockedThing].window.opened);
          }
          </script>

          <button id="door" onclick="knocking(event)">A door</button>

          </body>
          </html>





          <html>
          <body>
          <script>
          var door = {"opened":false, "window":{"opened":false} };

          function knocking(event){
          var knockedThing = event.target.id;

          alert(knockedThing);
          alert("door is opened : " + window[knockedThing].opened);
          alert("door window is opened : " + window[knockedThing].window.opened);
          }
          </script>

          <button id="door" onclick="knocking(event)">A door</button>

          </body>
          </html>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 16:08









          melpomene

          56.8k54487




          56.8k54487












          • Thanks Melpomene! I am not realy sure to understand what you mean. Isn't your solution what I am also suggesting thru my house[something] example in the second part of my message? What I would like to know is if there is a solution without to have to use the [ ] notation. But maybe it's not possible
            – mosis
            Nov 12 at 11:21












          • @mosis Why do you not want to use [ ]?
            – melpomene
            Nov 12 at 21:08










          • Hi, because I found more clear and confortable to access the object door directly by inputing door instead to have to create a parent object including my doorobject as property. And then to have to input parentObject[door] to acces it. And then I think I can't pass parentObject[door] directly as parameter in function(or can I ?)
            – mosis
            Nov 13 at 9:24










          • @mosis Why do you think you need to create a parent object? Did you not read my answer?
            – melpomene
            Nov 13 at 18:40










          • Hi melpomene, I do read your answer wich is working well. Maybe "parent object" is not the correct term to express that the variable parentObject is including door (making door a kind of child, by becoming a property). I don't know, as I am not an experienced programmer, I was thinking that working without [ ] if possible, would help to maintain my script clean.
            – mosis
            Nov 15 at 11:06




















          • Thanks Melpomene! I am not realy sure to understand what you mean. Isn't your solution what I am also suggesting thru my house[something] example in the second part of my message? What I would like to know is if there is a solution without to have to use the [ ] notation. But maybe it's not possible
            – mosis
            Nov 12 at 11:21












          • @mosis Why do you not want to use [ ]?
            – melpomene
            Nov 12 at 21:08










          • Hi, because I found more clear and confortable to access the object door directly by inputing door instead to have to create a parent object including my doorobject as property. And then to have to input parentObject[door] to acces it. And then I think I can't pass parentObject[door] directly as parameter in function(or can I ?)
            – mosis
            Nov 13 at 9:24










          • @mosis Why do you think you need to create a parent object? Did you not read my answer?
            – melpomene
            Nov 13 at 18:40










          • Hi melpomene, I do read your answer wich is working well. Maybe "parent object" is not the correct term to express that the variable parentObject is including door (making door a kind of child, by becoming a property). I don't know, as I am not an experienced programmer, I was thinking that working without [ ] if possible, would help to maintain my script clean.
            – mosis
            Nov 15 at 11:06


















          Thanks Melpomene! I am not realy sure to understand what you mean. Isn't your solution what I am also suggesting thru my house[something] example in the second part of my message? What I would like to know is if there is a solution without to have to use the [ ] notation. But maybe it's not possible
          – mosis
          Nov 12 at 11:21






          Thanks Melpomene! I am not realy sure to understand what you mean. Isn't your solution what I am also suggesting thru my house[something] example in the second part of my message? What I would like to know is if there is a solution without to have to use the [ ] notation. But maybe it's not possible
          – mosis
          Nov 12 at 11:21














          @mosis Why do you not want to use [ ]?
          – melpomene
          Nov 12 at 21:08




          @mosis Why do you not want to use [ ]?
          – melpomene
          Nov 12 at 21:08












          Hi, because I found more clear and confortable to access the object door directly by inputing door instead to have to create a parent object including my doorobject as property. And then to have to input parentObject[door] to acces it. And then I think I can't pass parentObject[door] directly as parameter in function(or can I ?)
          – mosis
          Nov 13 at 9:24




          Hi, because I found more clear and confortable to access the object door directly by inputing door instead to have to create a parent object including my doorobject as property. And then to have to input parentObject[door] to acces it. And then I think I can't pass parentObject[door] directly as parameter in function(or can I ?)
          – mosis
          Nov 13 at 9:24












          @mosis Why do you think you need to create a parent object? Did you not read my answer?
          – melpomene
          Nov 13 at 18:40




          @mosis Why do you think you need to create a parent object? Did you not read my answer?
          – melpomene
          Nov 13 at 18:40












          Hi melpomene, I do read your answer wich is working well. Maybe "parent object" is not the correct term to express that the variable parentObject is including door (making door a kind of child, by becoming a property). I don't know, as I am not an experienced programmer, I was thinking that working without [ ] if possible, would help to maintain my script clean.
          – mosis
          Nov 15 at 11:06






          Hi melpomene, I do read your answer wich is working well. Maybe "parent object" is not the correct term to express that the variable parentObject is including door (making door a kind of child, by becoming a property). I don't know, as I am not an experienced programmer, I was thinking that working without [ ] if possible, would help to maintain my script clean.
          – mosis
          Nov 15 at 11:06




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233780%2fcant-access-an-object-through-its-name-as-a-string%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Schultheiß

          Liste der Kulturdenkmale in Wilsdruff

          Android Play Services Check