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











up vote
1
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
    1
    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
      1
      down vote

      favorite









      up vote
      1
      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

      55.6k54387




      55.6k54387










      asked Nov 9 at 21:58









      mosis

      235




      235
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote
















          <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
            2 days ago












          • @mosis Why do you not want to use [ ]?
            – melpomene
            2 days ago










          • 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
            yesterday










          • @mosis Why do you think you need to create a parent object? Did you not read my answer?
            – melpomene
            yesterday











          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
































          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote
















          <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
            2 days ago












          • @mosis Why do you not want to use [ ]?
            – melpomene
            2 days ago










          • 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
            yesterday










          • @mosis Why do you think you need to create a parent object? Did you not read my answer?
            – melpomene
            yesterday















          up vote
          0
          down vote
















          <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
            2 days ago












          • @mosis Why do you not want to use [ ]?
            – melpomene
            2 days ago










          • 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
            yesterday










          • @mosis Why do you think you need to create a parent object? Did you not read my answer?
            – melpomene
            yesterday













          up vote
          0
          down vote










          up vote
          0
          down vote












          <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

          55.6k54387




          55.6k54387












          • 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
            2 days ago












          • @mosis Why do you not want to use [ ]?
            – melpomene
            2 days ago










          • 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
            yesterday










          • @mosis Why do you think you need to create a parent object? Did you not read my answer?
            – melpomene
            yesterday


















          • 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
            2 days ago












          • @mosis Why do you not want to use [ ]?
            – melpomene
            2 days ago










          • 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
            yesterday










          • @mosis Why do you think you need to create a parent object? Did you not read my answer?
            – melpomene
            yesterday
















          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
          2 days ago






          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
          2 days ago














          @mosis Why do you not want to use [ ]?
          – melpomene
          2 days ago




          @mosis Why do you not want to use [ ]?
          – melpomene
          2 days ago












          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
          yesterday




          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
          yesterday












          @mosis Why do you think you need to create a parent object? Did you not read my answer?
          – melpomene
          yesterday




          @mosis Why do you think you need to create a parent object? Did you not read my answer?
          – melpomene
          yesterday


















           

          draft saved


          draft discarded



















































           


          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




















































































          Popular posts from this blog

          List item for chat from Array inside array React Native

          Thiostrepton

          Caerphilly