I am new in Java Script. In both of my books: and
both authors are saying that this notation: object[unknownYetProperty]
should work when we have object and variable which is objects future property. The problem is two others notation works object['unknownYetProperty']
and object.unknownYetProperty
but not first one.
var a1 = 'spring';
var a2 = 'autumn';
var a3 = 'summer';
var object = {
propertyA1 : a1, // according to books this only this should work :-/
propertyA2 : a2,
propertyA3 : a3
}
console.log(object[propertyA1] + ' ' + object['propertyA2'] + ' ' + object.propertyA3);
The working example:
Could anyone explain it to me?
I am new in Java Script. In both of my books: http://www.larryullman./books/modern-javascript-develop-and-design and http://www.packtpub./object-oriented-javascript/book
both authors are saying that this notation: object[unknownYetProperty]
should work when we have object and variable which is objects future property. The problem is two others notation works object['unknownYetProperty']
and object.unknownYetProperty
but not first one.
var a1 = 'spring';
var a2 = 'autumn';
var a3 = 'summer';
var object = {
propertyA1 : a1, // according to books this only this should work :-/
propertyA2 : a2,
propertyA3 : a3
}
console.log(object[propertyA1] + ' ' + object['propertyA2'] + ' ' + object.propertyA3);
The working example: http://jsfiddle/cachaito/p78Le
Could anyone explain it to me?
Share Improve this question edited May 2, 2013 at 11:17 ChrisF♦ 137k31 gold badges264 silver badges334 bronze badges asked May 2, 2013 at 11:09 cachaitocachaito 3431 gold badge6 silver badges19 bronze badges 1- 1 There's nothing to explain, it's perfectly normal it isn't working. Notations 2 and 3 are indeed correct – Laurent S. Commented May 2, 2013 at 11:12
4 Answers
Reset to default 4The problem with object[propertyA1]
is that propertyA1
does not exist.
Basically, it's like doing the following (which doesn't work either): alert(propertyA1);
In contrast with the other two, which correctly reference the fields on your object:
object['propertyA2'] === object.propertyA2 === a2 == 'autumn'
object.propertyA3 == a3 == 'summer'
This is how it would have worked:
var propertyA1 = 'propertyA1',
temp = object[propertyA1]; // === 'spring'
propertyAl
doesn't exist. The reason the string literal and dot notation work is because you are correctly accessing the properties of the object. In the first one, you are trying to access object[undefined]
because propertyA1 isn't defined.
Well, when you write this:
object['propertyA1']
Javascript tries to find a property called propertyA1
inside object.
But when you use:
object[propertyA1]
Javascript tries to find a property inside object called... uh, lets see what's inside var propertyA1
... WTF? there is no variable with that name!! ok, then propertyA1 is undefined so:
object[undefined]
The difference is that Javascript knows what 'propertyA1' is (a string xD) but when you remove the quotes, that's not a string, that's a variable you have not declared yet.
The property you are accessing using the array notation should be specified as a string.
object['propertyA1']
is the right way to access the property of the object.
Whenever you create an object, the left side of the colon symbol is always a string. Hence, when accessing using array notation, strings should be used for properties.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742343834a4426147.html
评论列表(0条)