Access methods of JavaScript class attributes
var fish = {
head : 1,
tail : 1,
feature : {
speak : false,
swim : true
}
}
One , dot operator:
console.log(fish.head );//1
console.log(fish.tail);//1
console.log(fish.feature);//Object { head:1, tail:1, feature: Object}
Second, [] operator:
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:
console.log(fish[head]);//Error!
So, is the following code correct?
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