属性访问器有多种写法
一、像C#写实体类一样的写法
var attr={ ???????$x:10,//必须$开头 ???????get x() { ???????????return this.$x+1; ???????}, ???????set x(val) { ???????????this.$x=val+2; ???????} ???} ???console.log(attr.x); //11 ???attr.x=21; ???console.log(attr.x);//24
二、setAttribute\getAttribute
var shoop=document.getElementsById("psdf‘);shoop.setAttribute("tittle","a lot of goods")
三、用Objct.defineProperty
var stu={ _age=20;editor=1}Object.defineProperty(stu,"age",{ ???get:function(){ ???return this._age; ???}, ???set:function(newage){ ???????this._age=newage; ???????this.editor++; ???}})stu.age=200;
js 属性getset
原文地址:https://www.cnblogs.com/wangSOA/p/9744202.html