Home > Web Front-end > JS Tutorial > body text

Detailed explanation of access methods of JavaScript class attributes_Basic knowledge

WBOY
Release: 2016-05-16 17:00:48
Original
1204 people have browsed it

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'.
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template