Js object property call distinction. And [] in two ways

JS call the property there are two ways - point brackets and methods. 

Is a standard format object properties (without the quotation marks), attention to the fact that: js object's attributes, key standards are not quoted, you can also add, special circumstances must be added, if key figures ah, ah, etc. Expressions Wait.

Quotes is to be considered as a whole, we know one thing: From the perspective of the object, the property is not in quotation marks, such as the name, "name" This is what the property can not be a "name", even if they are double quotes, the object when you call or to remove quotes 

 

E.g

Instructions

var obj = {
    name: "cedric"
}

console.log(obj.name); // cedric

 

Instructions

var obj = {
    name: "cedric"
}

console.log(obj["name"]); // cedric

 

The point is to keep the property name after the object name, with the same attribute name in parentheses --- string method in the index stored.

 

Difference between the two

1. The point behind with the method must be a specified attribute name, and the method can be a variable brackets. E.g

var haha = "name";
console.log(obj.haha); // undefined
console.log(obj[haha]); // cedric

 

2. The method of brackets which can be a digital attribute name, and the latter point is not a method of digital property name

3. When an object dynamically add attributes, brackets [] must be used, with the point method is not available

 

Guess you like

Origin www.cnblogs.com/ll15888/p/11904707.html