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);
javascript variables
add a comment |
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);
javascript variables
add a comment |
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);
javascript variables
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
javascript variables
edited Nov 10 at 16:05
melpomene
56.8k54487
56.8k54487
asked Nov 9 at 21:58
mosis
237
237
add a comment |
add a comment |
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']
.
Thanks Melpomene! I am not realy sure to understand what you mean. Isn't your solution what I am also suggesting thru myhouse[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 objectdoor
directly by inputingdoor
instead to have to create a parent object including mydoor
object as property. And then to have to inputparentObject[door]
to acces it. And then I think I can't passparentObject[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 variableparentObject
is includingdoor
(makingdoor
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
|
show 4 more comments
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']
.
Thanks Melpomene! I am not realy sure to understand what you mean. Isn't your solution what I am also suggesting thru myhouse[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 objectdoor
directly by inputingdoor
instead to have to create a parent object including mydoor
object as property. And then to have to inputparentObject[door]
to acces it. And then I think I can't passparentObject[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 variableparentObject
is includingdoor
(makingdoor
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
|
show 4 more comments
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']
.
Thanks Melpomene! I am not realy sure to understand what you mean. Isn't your solution what I am also suggesting thru myhouse[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 objectdoor
directly by inputingdoor
instead to have to create a parent object including mydoor
object as property. And then to have to inputparentObject[door]
to acces it. And then I think I can't passparentObject[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 variableparentObject
is includingdoor
(makingdoor
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
|
show 4 more comments
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']
.
<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>
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 myhouse[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 objectdoor
directly by inputingdoor
instead to have to create a parent object including mydoor
object as property. And then to have to inputparentObject[door]
to acces it. And then I think I can't passparentObject[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 variableparentObject
is includingdoor
(makingdoor
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
|
show 4 more comments
Thanks Melpomene! I am not realy sure to understand what you mean. Isn't your solution what I am also suggesting thru myhouse[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 objectdoor
directly by inputingdoor
instead to have to create a parent object including mydoor
object as property. And then to have to inputparentObject[door]
to acces it. And then I think I can't passparentObject[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 variableparentObject
is includingdoor
(makingdoor
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 door
object 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 door
object 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
|
show 4 more comments
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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