0

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);

1 Answer 1

1

<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'].

Sign up to request clarification or add additional context in comments.

9 Comments

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 Why do you not want to use [ ]?
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 Why do you think you need to create a parent object? Did you not read my answer?
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.