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

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -