(Sourced from here .)
((从此处获取 。))
Square bracket notation allows the use of characters that can't be used with dot notation:
(方括号表示法允许使用点符号不能使用的字符:)
var foo = myForm.foo[]; // incorrect syntax var foo = myForm["foo[]"]; // correct syntax
including non-ASCII (UTF-8) characters, as in myForm["ダ"]
( more examples ).
(包括非ASCII(UTF-8)字符,如myForm["ダ"]
( 更多示例 )。)
Secondly, square bracket notation is useful when dealing with property names which vary in a predictable way:
(其次,方括号表示法在处理以可预测的方式变化的属性名称时非常有用:)
for (var i = 0; i < 10; i++) { someFunction(myForm["myControlNumber" + i]); }
Roundup:
(围捕:)
Another example of characters that can't be used with dot notation is property names that themselves contain a dot .
(不能与点符号一起使用的字符的另一个示例是本身包含点的属性名称 。)
For example a json response could contain a property called bar.Baz
.
(例如,一个json响应可能包含一个名为bar.Baz
的属性。)
var foo = myResponse.bar.Baz; // incorrect syntax
var foo = myResponse["bar.Baz"]; // correct syntax
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…