JavaScript class attribute access method
Copy code code as follows:
var Fish = {{
head: 1,
tail: 1,
Feature: {{
speak: false,
SWIM: TRUE
}
}
First, click the operating symbol:
Copy code code 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 code as follows:
console.log (fish ['head']); // 1
One thing to pay attention at this time is: the attribute name must be a string form
like:
Copy code code as follows:
console.log (fish [head]); // error!
So is the following code correct?
Copy code code as follows:
for (var Prop in Fish) {
console.log (fish [prop]);
}
The answer is yes. This is because when traversing the object attribute, it exists in the string type, that is, the PROP is 'head', 'tail', 'Feature'.