javascript mxins 使用`Object.assign`,比预期更早的评估

问题描述

我有一些带有 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 ...

我检查了调用堆栈,这表明 [1] 行正在调用 getter。

我在谷歌上做了很多搜索,完全迷失了。这个 getter 在被合并到我的原型中时被评估??这比我预期的要早得多。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)