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'.