This question already has an answer here:
Why does first work and not the latter?? *its only a minor difference where in the latter case i used the shorthand for accessing cats object property. I read that it shouldn't make any difference if "name of the property would be a valid variable name ― when it doesn't have any spaces or symbols in it and does not start with a digit character."
//this works
var cats = {Spot:true};
function addCat (name) { cats[name] = true; }
addCat("white");
console.log ("white" in cats); //true
console.log (cats.white); //true
//this doesn't work
var cats = {Spot:true};
function addCat (name) { cats.name = true; }
addCat("white");
console.log ("white" in cats); //false
console.log (cats.white); //undefined
cats.name
is not dynamic. – Shawn31313 Jul 18 '13 at 19:01console.log(cats)
and you'll see your problem – Bergi Jul 18 '13 at 19:03cats.name
sets a property called'name'
oncats
. – Rocket Hazmat Jul 18 '13 at 19:05[]
) and dot (.
) notation – Felix Kling Jul 18 '13 at 19:08