Home  >  Article  >  Web Front-end  >  Detailed explanation of access methods of JavaScript class attributes_Basic knowledge

Detailed explanation of access methods of JavaScript class attributes_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:00:481175browse

Access methods of JavaScript class attributes

Copy code The code is as follows:

var fish = {
head : 1,
tail : 1,
feature : {
speak : false,
swim : true
}
}

One , dot operator:
Copy code The code is as follows:

console.log(fish.head );//1
console.log(fish.tail);//1
console.log(fish.feature);//Object { head:1, tail:1, feature: Object}

Second, [] operator:
Copy code The code is as follows:

console.log(fish['head']);//1

One thing to note at this time is: the attribute name must be in the form of a string
such as:
Copy code The code is as follows:

console.log(fish[head]);//Error!

So, is the following code correct?
Copy code The code is as follows:

for(var prop in fish) {
console .log(fish[prop]);
}

The answer is yes, this is because when traversing the object properties, they exist in string type, that is, the props are 'head', 'tail','feature'.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn