javascript - dynamically adding a number as a property to an existing object -


in javascript, when adding property existing object this:

var qbacks = {   12: "namath",   16: "montana",   19: "unitas" };  qbacks["4"] = "brett favre"; //will work! qbacks.4 = "brett favre"; //will not work! //but qbacks.player4 = "brett favre"//will work. 

and, if want append property 4 remove first name, have use bracket notation complete:

qbacks[4] = "farve"; //works! qbacks.4 = "farve"; //will not work! 

why won't dot operator work numbers dynamically add properties or modify value? guessing has typeof 4 being primitive better understanding. thanks

a numeric key on objects converted string. done in step 6 of ecmascript property accessor (§11.2.1) algorithm.

this answered here

var foo = {}; foo[4] = 'bar';  console.log(foo[4] === foo["4"]); // returns true 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -