js对象的set/get方法案例

代码 :

var obj = {name:'jzx',
		_age:18,   //属性前加_建议不要直接访问
		get age(){
			return this._age;
		},
		set age(val){
			this._age = val;
		}
	};
console.log(obj._age);  //18
console.log(obj.age);   //18
obj.age = 20;  //赋值
console.log(obj.age);  //20

你可能感兴趣的:(【JavaScript笔记】,对象,javascript,setter/getter)