Javascript square brackets have four semantics
Semantics 1, declare array
Copy the code code as follows:
var ary = []; // Declare an empty array
var ary = [1,3]; // Declare an array and assign an initial value
Semantic 2, get array members
Copy the code code as follows:
var ary = [1,2,3];
var item = ary[0];
Semantic 3, define object members (can not follow identifier rules)
Copy the code code as follows:
var obj = {};
//Add an attribute name to obj. name is a legal identifier, that is, it can also be defined through obj.name.
obj['name'] = 'jack';
//Add an attribute 2a to obj. 2a is not a legal identifier (cannot start with a number) and cannot be defined through obj.2a
obj['2a'] = 'test';
Semantics 4, get object members
Copy the code code as follows:
var obj = {name:'jack'};
obj['2a'] = 'test';
obj['name']; // --> jack
obj['2a']; // --> test (cannot be obtained through obj.2a)