屬性訪問器
一、像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;
屬性標簽
- configurable //是否可以修改屬性標簽或delete
- writable //是否可寫
- enumerable //是否可枚舉(遍歷for的時候必須是可枚舉的或object.keys(objs))
- get/set 訪問器,上面寫過了(getset訪問器=>看三object.defineProperty)