js中set和get方法

get和set的使用方法:

1. get和set是方法,因为是方法,所以可以进行判断

2. get一般是要返回的;而set是设置,不用返回

3. 如果调用对象内部的属性约定的命名方式是变量名前加_

var p={
    _age:18,
    get age(){
        return this._age;
    },
    set age(val){
        this._age=val;
    }
}
p.age();    // 18
p.age=20;    // 20
p.age;    // 20

即get是对象的属性值返回的方法,set是属性值修改的方法

猜你喜欢

转载自blog.csdn.net/qq_41049816/article/details/88795343