This is a basic question about the JavaScript (ECMAScript) language so I apologize in advance if it's a duplicate (a little searching didn't reveal my exact question).
这是一个关于JavaScript(ECMAScript)语言的基本问题,所以如果它是重复的话我会提前道歉(有点搜索没有透露我的确切问题)。
In ECMAScript we can use two basic syntactic forms to get/set properties on an object and they seem to have the exact same effect. Since I don't know better, I'll call them "property" and "associative array" notations:
在ECMAScript中,我们可以使用两种基本的句法形式来获取/设置对象的属性,它们似乎具有完全相同的效果。由于我不知道更好,我会称它们为“属性”和“关联数组”符号:
var o = {};
// Property notation.
o.foo = 'Foo'; // (set)
o.foo; // => "Foo" (get)
// Associative array notation.
o['bar'] = 'Bar'; // (set)
o['bar']; // => "Bar" (get)
// They seem to be interchangeable.
o['foo']; // => "Foo"
o.bar; // => "Bar"
Are there any real differences between these two notations? Obviously, the associative array notation allows us to lookup dynamically generated keys on the object (and forcibly casts its argument to a string) whereas the property notation uses a literal but is that the only difference?
这两种符号之间是否存在真正的差异?显然,关联数组表示法允许我们在对象上查找动态生成的键(并强制将其参数强制转换为字符串),而属性表示法使用文字,但这是唯一的区别吗?
1 个解决方案
#1
5
You are correct; they are identical.
你是对的;他们是相同的。
#1
5
You are correct; they are identical.
你是对的;他们是相同的。