Open HuangHongRui opened 7 years ago
class CustomHTMLEle {
constructor(ele){
this.ele = ele
}
get html() {
return this.ele.innerHTML;
}
set html(val) {
this.ele.innerHTML = val;
}
}
let descriptor = Object.getOwnPropertyDescriptor(CustomHTMLEle.prototype, 'html');
console.log( 'get' in descriptor) // true
console.log( 'set' in descriptor) // true
等同上面Code
let Cust = (function(){
'use strict'
const Cust = function(ele){
if( typeof new.target === "undefined") {
throw new Error("必须new调用")
}
this.ele = ele
}
Object.defineProperty(Cust.prototype, 'html', {
enumerable : false,
configurable : true,
get : function() {
return this.ele.innerHTML;
},
set : function(val) {
this.ele.innerHTML = val
}
})
return Cust;
}())
类 也支持直接在原型上定义 访问器属性.
创建
getter
/setter
时, 在关键后紧跟一个空格和相应标识符.