Difference between using bracket (`[]`) and dot (`.`) notation
来源:互联网 发布:sublime 解压缩js 编辑:程序博客网 时间:2024/06/05 06:57
http://stackoverflow.com/questions/17189642/difference-between-using-bracket-and-dot-notation
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 Valid Characters 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; // validobj.else // valid, reserved keywords are valid identifier namesobj.42 // invalid, identifier names cannot start with numbersobj.3foo // invalid, ""obj.foo-bar // invalid, `-` is not allowed in identifier namesobj[42] // valid, 42 will be coerced to "42"obj["--"] // valid, any character sequence is allowedobj[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.
- Difference between using bracket (`[]`) and dot (`.`) notation
- Dot notation vs. bracket notation when retriving object property values
- What is the difference between angle bracket < > and double quotes " " while including header files
- The difference between (()) and ().
- difference between "<" and "<<"
- Difference Between typedef and #define
- Difference between PCDATA and CDATA
- Difference between CComPtr and CComQIPtr
- Difference between RVCT and GCCE
- Difference between RVCT and GCCE
- difference between slist and list.
- difference between stack and heap
- Difference between replace and translate
- Difference between PrintWriter and PrintStream
- difference between Cookie and Session
- Difference Between Buffer and Cache
- Difference between Statement and preparedStatement
- Difference between .DLL And .LIB
- iOS 常用的宏
- iOS app 适配 iPhone6/iPhone6 plus 遇到的问题
- 求任意个点的凸包问题
- windows 2003 安装mysql odbc失败 error1918
- hdu4505 小Q系列故事——电梯里的爱情(水题)
- Difference between using bracket (`[]`) and dot (`.`) notation
- scrollview的使用
- 利用iptables来配置linux禁止所有端口登陆和开放指定端口(干货)
- @SuppressWarnings的使用、作用、用法
- 数据结构-Vector实现(C++)
- nodejs代码初探
- Hadoop Hive sql语法详解
- ERROR ITMS-9000: "Redundant Binary Upload. There already exists a binary upload with build version '
- OJ刷题之迭代法求平方根