On the Object Access attribute points represent the difference between the law and the array notation

Value is generally used. "" Operator to access properties of an object in JavaScript. Or using [] as an associative array to access the object's properties. But what's the difference of these two approaches?

For example, read the property value in the property object:

    object.property

    object['property']

Access attribute above two methods both can be achieved.

1. The difference between the grammar of

Point represents a property name is the identifier of an object method, while the latter is a property name string.

2. The difference in flexibility

In the JavaScript programming, you can create any number of attributes for the object. However, the "." Operator is used to access properties of an object, the property name is the identifier represents. In JavaScript program identifier literally must be input, they are not a data type, its operation and therefore the program can not. In other words, the identifier is static, it must be hard-coded in the program.

Using the array [] represents a method to access when an object's attributes, the attribute name is a string. JavaScript is a string data type, and thus create them can be operated in the program run.

3. The performance difference

Array [] notation when access attribute values ​​will be run expression. And the dotted notation is direct access attribute value, high efficiency method theoretically would represent a ratio of the array. In fact, performance can be ignored.

Some scenes must use array notation to access a dynamic property value, this is the point representation can not be done.

Overall, not much different on the two methods, there is a corresponding usage scenarios. Usually dot notation when used as a static object to access properties. The array notation is useful when dynamic access attribute. Unless usually must use variables to access the property, it is recommended to use dot notation.

-----------------------------

 

Published an original article · won praise 2 · views 10000 +

Guess you like

Origin blog.csdn.net/u011927449/article/details/104052745