在 JavaScript 中,当向现有对象添加属性时,如下所示:
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.
而且,如果我想附加属性 4 来删除名字,我必须使用括号符号来完成:
qBacks[4] = "Farve"; //Works!
qBacks.4 = "Farve"; //Will not work!
为什么点运算符不使用数字来动态添加属性或修改值?我猜这与 typeof 4 是原始的有关,但希望得到更好的理解。谢谢