【发布时间】:2021-01-14 07:57:00
【问题描述】:
我有 fowlling mixin,带有一个 setter 和一个 getter:
const dataentity_compnts_mixins = {
set within_context(val_obj) {
switch (val_obj["dataentity_morphsize"]) {
case 'compact':
this.shadow_wrapper.setAttribute(
"within-context", "compact_dataentity");
break;
case 'expanded':
this.shadow_wrapper.setAttribute(
"within-context", "expanded_dataentity");
break;
}
},
get within_context() {
const host_dataentity = this.parent_dataentity ||
this.closest('independent-data-entity') ||
this.closest('dependent-data-entity');
this.parent_dataentity = host_dataentity;
const context_dict = {
"dataentity_morphsize": host_dataentity.getAttribute("morph-size"),
"dataentity_role": host_dataentity.getAttribute("entity-role")
};
return context_dict;
}
};
然后我使用Object.assign 将其合并到我的自定义元素的原型中:
Object.assign(IndividualviewEditor.prototype, dataentity_compnts_mixins); [1]
我期望 getter 和 setter 不会被评估,直到被 this 引用主机对象调用,在我的例子中是 IndividualviewEditor 自定义元素。但是,在我的网页上运行此代码时,出现错误:
Uncaught TypeError: this.closest is not a function ...
我检查了调用堆栈,这表明 getter 正在被行 [1] 调用。
我在 Google 上进行了多次搜索,但完全迷失了方向。这个吸气剂在合并到我的原型时被评估??这比我预期的要早得多。
【问题讨论】:
标签: javascript mixins getter