我有一个JS Custom Element属性
static get observedAttributes() {
return ['state','color'];
}
我怎样才能在自定义元素的Callback
函数,整个阵列?
this.observedAttributes()
不是一个函数
我必须忽视的东西容易...
我总是忘记的Getter和Setter
我现在可以做到这一点在构造函数():
this.constructor.observedAttributes.map(attribute =>
Object.defineProperty(this, attribute, {
get() {
return obj.getAttribute(attribute);
},
set(newValue) {
obj.setAttribute(attribute, newValue);
},
enumerable: true,//default:false
configurable: true // default:false
//writable: true // not valid, since there is a set method!
}));
(不关心内存消耗副作用)
observedAttributes
被定义为static
所以它会被取消的类,而不是一个实例。 observedAttributes
也是getter(get
),所以你不会()
执行它。如果您定义的自定义元素类,你应该使用FancyButton
FancyButton.observedAttributes
。