Parsing the ambiguity _javascript technique of bracket "[]" in JavaScript

Source: Internet
Author: User

JavaScript has four semantics in parentheses

Semantic 1, declaring an array

Copy Code code as follows:

var ary = []; Declare an empty array
var ary = [1,3]; Declare an array, and assign an initial value

Semantic 2, taking an array member
Copy Code code as follows:

var ary = [1,2,3];
var item = ary[0];

Semantic 3, defining an object member (you may not follow an identifier rule)
Copy Code code as follows:

var obj = {};

Adding a property to obj Name,name is a valid identifier, that is, it can also be defined by the Obj.name method
obj[' name ' = ' Jack ';

Adding a property to obj 2a,2a is not a valid identifier (cannot begin with a number) and cannot be defined by obj.2a
obj[' 2a '] = ' test ';


Semantic 4, fetching object members
Copy Code code as follows:

var obj = {name: ' Jack '};
obj[' 2a '] = ' test ';

obj[' name ']; -->, Jack.
obj[' 2a ']; --> test (not available through obj.2a)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.