gpt4 book ai didi

javascript - 在类定义中使用 mixin 后无法覆盖 mixin 行为

转载 作者:行者123 更新时间:2023-12-02 20:57:34 28 4
gpt4 key购买 nike

我需要重写 mixin 添加的一些成员函数来自第三方库。问题是:mixin 立即在多个第 3 方类定义中使用,在定义 mixin 的同一个脚本文件中。我只能在此脚本之前或之后插入自定义代码,而不能在两者之间插入自定义代码。如果我之后调用override,那么已经定义的类不会在调用链中获取我的函数。

// library script BEGIN
Ext.define('Foo.bar.Base', {
});


Ext.define('Foo.bar.Util', {
newmember: function() {
console.log('newmember');
}
});


Ext.define('Foo.bar.Derived', {
extend: 'Foo.bar.Base',

mixins: {
fooutil: 'Foo.bar.Util'
}
});

// library script END

Foo.bar.Util.override({
newmember: function () {
console.log('newmember2');
this.callParent();
}
});


var obj = new Foo.bar.Derived();

obj.newmember();

实际输出:

newmember

期望的输出:

newmember2
newmember

最佳答案

在使用 mixin 定义类之前进行覆盖。这可以使用 override 来完成定义时作为属性:


Ext.define('Foo.bar.UtilOverride',{
override: 'Foo.bar.Util',
newmember: function () {
console.log('newmember2');
this.callParent();
}
});

// library script BEGIN
Ext.define('Foo.bar.Base', {
});


Ext.define('Foo.bar.Util', {
newmember: function() {
console.log('newmember');
}
});


Ext.define('Foo.bar.Derived', {
extend: 'Foo.bar.Base',

mixins: {
fooutil: 'Foo.bar.Util'
}
});

// library script END



var obj = new Foo.bar.Derived();

obj.newmember();

关于javascript - 在类定义中使用 mixin 后无法覆盖 mixin 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61436441/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com