使用括号(`[]`)和点(`。')符号之间的区别
使用[]
和。
用于访问数组或对象属性的真正区别是什么?要使用哪一个?
另外,为什么。
操作员允许索引属性?
What is the real difference in using []
and .
for accessing array or object properties? Which one to use?
Also why doesn't .
operator allow the index property?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
。
访问成员被称为 dot note法。使用[]
访问它们,称为括号符号。点表示法仅适用于有效的属性名称 标识符名称 [[ spec] ,因此,基本上任何也是有效变量名称的任何名称(有效的标识符,另请参见哪些字符对于JavaScript变量名称有效?)和任何
括号符号期望将评估字符串的表达式(或可以将其胁迫到字符串),因此您可以将任何字符序列用作属性名称。字符串可以包含的内容没有限制。
示例:
使用括号符号:
obj [foo]
。-
),例如obj [“我的属性“]
。使用点符号:在所有其他情况下。
关于保留的关键字,有一个警告。尽管规范允许将它们用作属性名称并使用点表示法,但并非所有浏览器或工具都尊重此(尤其是较旧的IE版本)。因此,我认为最好的解决方案是避免使用保留的关键字作为属性名称,或者如果不能,则使用括号符号。
†:这也是您只能使用括号符号访问数组元素的原因。标识符不能从数字开始,因此不能仅由数字组成。
Accessing members with
.
is called dot notation. Accessing them with[]
is called bracket notation.The dot notation only works with property names which are valid identifier names [spec], so basically any name that would also be a valid variable name (a valid identifier, see also What characters are valid for JavaScript variable names?) and any reserved keyword [spec].
Bracket notation expects an expression which evaluates to a string (or can be coerced to a string), so you can use any character sequence as property name. There are no limits to what a string can contain.
Examples:
Use bracket notation:
obj[foo]
.-
), e.g.obj["my property"]
.Use dot notation: In all other situations.
There is a caveat though regarding reserved keywords. While the specification permits to use them as property names and with the dot notation, not all browsers or tools respect this (notably older IE versions). So the best solution in my opinion is to avoid using reserved keywords for property names or use bracket notation if you cannot.
†: That's also the reason why you can only use bracket notation to access array elements. Identifiers cannot start with digits, and hence cannot consist only of digits.
您应该使用
。
知道属性的名称时,请使用
[]
当属性的名称包含在变量中时,因为@dcoder添加了某些对象属性无法访问而无需使用
[]
符号,因为它们的名称打破了语法。例如,属性<代码> class ,默认
或data-prop-value的属性You should use
.
when you know the name of the property, use
[]
when the name of the property is contained in a variableAs @DCoder added certain object properties cannot be accessed without using the
[]
notation because their names break the syntax. E.g. properties namedclass
,default
, or data-prop-value好吧,如果可能的话,请考虑:
您是指数字
0.5
还是访问5
数字的元素?请参阅:
如果允许语法
0.5
等于0 [5]
,那么您如何知道您的意思?但是,可以直接将数字与对象文字使用:
Well if that was possible, consider:
Did you mean the number
0.5
or access the5
element of the number?See:
If you allowed the syntax
0.5
to be equal to0[5]
, then how do you know what you mean?It is however possible to use numbers directly with object literal:
点运算符和索引(支架符号)操作员都用于访问对象的属性。通常,使用点运算符访问的速度要快得多,因为通过窗口访问变量的速度明显较慢。但是如果特殊性格
在变量中,您不能使用点运算符,因为它会出现错误。对于这种情况,我们需要使用索引运算符并将变量名称作为字符串格式传递,含义双引号否则会产生未定义的错误。
例如-
Both dot operator and index(bracket notation) operator are used to access the property of an Object. Generally accessing with dot operator is quite faster because accessing variables by window is significantly slower though. But in case of special character
in the variables, you cannot use dot operator as it will give error. For such cases we need to use index operator and pass the variable name as a string format means underdouble quote otherwise it will give undefined error.
e.g-