JS添加到功能而不是替换为原型


base = {
    init : function() {
        console.log('initiate')
    }
}
function Plugin(){
    this.init = function() {
        console.log('init me');
    }
}
Plugin.prototype = base;
x = new Plugin();
x.init();
我希望

它同时控制台记录,我希望能够在基本变量中设置一组默认函数,并在函数插件中添加它们。

https://jsfiddle.net/eqzwbdww/

不确定这是否是最好的解决方案,但解决了它:

base = {
    init : function() {
       console.log('initiate')
    }
}
function Plugin(){
    this.init = function() {
       Plugin.prototype.init.call(this);
       console.log('init me');
    }
}
Plugin.prototype = base;
x = new Plugin();
x.init();

另一种解决方案,也不确定这是否是最好的解决方案。

base = {
    init : function() {
        document.write(' initiate ')
    }
}
function Plugin(){
    this.init = function() {
    	this.__proto__.init ();
        document.write(' init me ');
    }
}
Plugin.prototype = base;
x = new Plugin();
x.init();

最新更新