使用括号(`[]`)和点(`。')符号之间的区别

发布于 2025-02-02 17:01:48 字数 107 浏览 2 评论 0原文

使用[]用于访问数组或对象属性的真正区别是什么?要使用哪一个?

另外,为什么操作员允许索引属性?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青巷忧颜 2025-02-09 17:01:48

使用访问成员被称为 dot note法。使用[]访问它们,称为括号符号

点表示法仅适用于有效的属性名称 标识符名称 [[ spec] ,因此,基本上任何也是有效变量名称的任何名称(有效的标识符,另请参见哪些字符对于JavaScript变量名称有效?)和任何

括号符号期望将评估字符串的表达式(或可以将其胁迫到字符串),因此您可以将任何字符序列用作属性名称。字符串可以包含的内容没有限制。

示例:

obj.foo;  // valid
obj.else  // valid, reserved keywords are valid identifier names
obj.42    // invalid, identifier names cannot start with numbers
obj.3foo  // invalid,                ""
obj.foo-bar // invalid, `-` is not allowed in identifier names

obj[42]   // valid, 42 will be coerced to "42"
obj["--"] // valid, any character sequence is allowed
obj[bar]  // valid, will evaluate the variable `bar` and 
          // use its value as property name

使用括号符号:

  • 当属性名称包含在变量中时,例如obj [foo]
  • 该属性名称包含标识符中不允许的字符,例如Digit ,或包含一个空间或仪表板(-),例如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:

obj.foo;  // valid
obj.else  // valid, reserved keywords are valid identifier names
obj.42    // invalid, identifier names cannot start with numbers
obj.3foo  // invalid,                ""
obj.foo-bar // invalid, `-` is not allowed in identifier names

obj[42]   // valid, 42 will be coerced to "42"
obj["--"] // valid, any character sequence is allowed
obj[bar]  // valid, will evaluate the variable `bar` and 
          // use its value as property name

Use bracket notation:

  • When the property name is contained in a variable, e.g. obj[foo].
  • The property name contains characters not permitted in identifiers, e.g. starts with a digit, or contains a space or dash (-), 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.

说谎友 2025-02-09 17:01:48

您应该使用知道属性的名称时

var object = {};
object.property = 'whatever';

,请使用[]当属性的名称包含在变量中时

var object = {};
var property = 'another-property';
object[property] = 'whatever';

,因为@dcoder添加了某些对象属性无法访问而无需使用[]符号,因为它们的名称打破了语法。例如,属性<代码> class ,默认或data-prop-value的属性

You should use . when you know the name of the property

var object = {};
object.property = 'whatever';

, use [] when the name of the property is contained in a variable

var object = {};
var property = 'another-property';
object[property] = 'whatever';

As @DCoder added certain object properties cannot be accessed without using the [] notation because their names break the syntax. E.g. properties named class, default, or data-prop-value

囍孤女 2025-02-09 17:01:48

也为什么不。操作员允许索引属性?我真的想要
完全理由。谢谢。

好吧,如果可能的话,请考虑:

var a = 0.5;

您是指数字0.5还是访问5数字的元素?
请参阅:

Number.prototype[5] = 3;

0[5] //3
0.5 // 0.5

如果允许语法0.5等于0 [5],那么您如何知道您的意思?

但是,可以直接将数字与对象文字使用:

var a = {
    0: 3,
    1: 5
};

Also why doesn't . operator allow the index property? I really want
full reason. Thank you.

Well if that was possible, consider:

var a = 0.5;

Did you mean the number 0.5 or access the 5 element of the number?
See:

Number.prototype[5] = 3;

0[5] //3
0.5 // 0.5

If you allowed the syntax 0.5 to be equal to 0[5], then how do you know what you mean?

It is however possible to use numbers directly with object literal:

var a = {
    0: 3,
    1: 5
};
执笔绘流年 2025-02-09 17:01:48

点运算符和索引(支架符号)操作员都用于访问对象的属性。通常,使用点运算符访问的速度要快得多,因为通过窗口访问变量的速度明显较慢。但是如果特殊性格
在变量中,您不能使用点运算符,因为它会出现错误。对于这种情况,我们需要使用索引运算符并将变量名称作为字符串格式传递,含义双引号否则会产生未定义的错误。
例如-

var abc = {
    font-size : "12px"
} 
Using dot operator - abc.font-size;           //it will give error (Incorrect)
Using index operator - abc["font-size"];      //12px (Correct)

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-

var abc = {
    font-size : "12px"
} 
Using dot operator - abc.font-size;           //it will give error (Incorrect)
Using index operator - abc["font-size"];      //12px (Correct)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文