Home > Web Front-end > JS Tutorial > JS get and set usage examples_Basic knowledge

JS get and set usage examples_Basic knowledge

WBOY
Release: 2016-05-16 16:59:07
Original
1020 people have browsed it

Using get and set skillfully, you can directly operate object attributes to read and write, which can greatly improve programming efficiency. Here is a typical example:

Copy code The code is as follows:

var test = {
_Name : null,
_Age : 0,

//Reading and writing of _Name
set name(name) {this._Name = name;},
get name() {return this._Name;},
//Reading and writing of _Age
set age(age) {this._Age = age;},
get age() {return this._Age;}
}

alert(test.name " " test.age);//bull 0
test. name = 'lucy';
test.age = 20;
alert(test.name " " test.age);//lucy 20
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