object - JavaScript and square bracket notation -
i new in java script. in both of books: http://www.larryullman.com/books/modern-javascript-develop-and-design , http://www.packtpub.com/object-oriented-javascript/book both authors saying notation: object[unknownyetproperty]
should work when have object , variable objects future property. problem 2 others notation works object['unknownyetproperty']
, object.unknownyetproperty
not first one.
var a1 = 'spring'; var a2 = 'autumn'; var a3 = 'summer'; var object = { propertya1 : a1, // according books should work :-/ propertya2 : a2, propertya3 : a3 } console.log(object[propertya1] + ' ' + object['propertya2'] + ' ' + object.propertya3);
the working example: http://jsfiddle.net/cachaito/p78le
could explain me?
propertyal
doesn't exist. reason string literal , dot notation work because correctly accessing properties of object. in first one, trying access object[undefined]
because propertya1 isn't defined.
Comments
Post a Comment