JavaScript在不知道特定父级的情况下调用父级中重写的函数是否有一个好的模式?

JavaScript在不知道特定父级的情况下调用父级中重写的函数是否有一个好的模式?,javascript,Javascript,基本上,我想要可继承的函数,如中所示 Base = function() { }; Base.prototype.foo = function() { console.log("base foo"); }; Derived = function() { }; somelib.inherit(Derived, Base); Derived.prototype.foo = function() { console.log("derived foo"); } d = new Deri

基本上,我想要可继承的函数,如中所示

Base = function() { };

Base.prototype.foo = function() {
  console.log("base foo");
};

Derived = function() { };

somelib.inherit(Derived, Base);

Derived.prototype.foo = function() {
  console.log("derived foo");
}

d = new Derived():
d.foo();
我想把它打印出来

derived foo
base foo
是的,我知道我可以显式地调用Base.prototype.foo.call(this);我只是想知道是否有一种模式可以自动调用重写的超类函数。我试图解决的问题有两个方面

  • 派生类不必记住调用其父类的方法,它只是自动发生的
  • 如果1。那是不可能的,至少我不想直呼Base的名字,因为那很脆弱。更确切地说,我希望它叫做parentclass之类的东西,这样你就不必知道它的基了。这样,如果更改基类的名称,就不必修复每个派生类

  • 据我所知,JavaScript中没有语言集成的析构函数功能。这都是关于框架的。例如,如果您使用的是ASP.NET Ajax,那么框架会期望您的对象具有dispose方法,负责释放资源(事件处理程序)。所以,这取决于您。

    好的,这不是您想要的,因为它不是一种“模式”,但它是一种您可以遵循的潜在实现路径:


    看看MooTools包(因为没有更好的词)。使用Chain类,您可能会获得所需的功能。

    您可以使用以下结构来实现此类功能:

    function Base(){}
    Base.prototype.destroy = function(){console.log('Base destroy');};
    
    function Derived(){}
    Derived.prototype = new Base; // Let Derived inherit from Base
    
    // Override the `destroy` method
    Derived.prototype.destroy = function() {
        console.log('Derived destroy');
    
        // Call parent class method
        this.constructor.prototype.destroy();
        // If the context of the method is important, you can use Function.call:
      //this.constructor.prototype.destroy.call(this);
    };
    
    
    // Create an instance of Derived, and call the destroy method:
    (new Derived).destroy();
    

    我更喜欢一种方式,不指定Derived=chainify(),这样api将与您在问题中遇到的api相同,但到目前为止,这是我能让它工作的最好方式。它的工作原理是用一个方法替换对象的每个方法,该方法调用被替换的方法,并沿着父链向上移动,同时调用它们的方法

    function chainify() {
        return function () {
            var property;
            for (property in this) {
                if (typeof this[property] === "function") {
                    this[property] = chain(this[property], property);
                }
            }
            function chain(method, method_name) {
                return function() {
                    method();
                    var current = this;
                    while (current = current.parent) {
                        if (current.hasOwnProperty(method_name)) {
                            current[method_name].apply(this, arguments);
                        }
                    }
                };
            }
        }
    }
    var somelib = function() { };
    
    somelib.inherit = function (derive, base) {
        derive.prototype = new base;
        derive.prototype.parent = base.prototype;
    };
    
    var Base = function() { };
    
    Base.prototype.foo = function() {
      console.log("base foo");
    };
    
    var Derived = chainify();
    
    somelib.inherit(Derived, Base);
    
    Derived.prototype.foo = function() {
      console.log("derived foo");
    };
    
    d = new Derived();
    d.foo();
    

    我建议您考虑一下为什么要这样做,至少是在需求方面。请记住,您所期望的模式将剥夺大量的灵活性。例如,如果您希望以相反的顺序打印语句:

        base foo
        derived foo
    
    您要么放弃模式,要么在派生类中创建一个函数foo2(),然后在基类中调用foo()。两者都不是很漂亮

    如果你想做一些简单的事情,比如:

        derived foo
        base foo
        one more thing in the derived function
    

    我认为,使用这种模式可能适用于您现在想要做的事情,但当您想要在未来做出看似微不足道的改变时,它可能会给您带来好处。全部保存一行代码

    在Javascript中,您真正需要析构函数的频率有多高?在长时间运行的应用程序中,例如通过ajax调用刷新数据时,您需要析构函数。为了避免出现问题,在不再需要对象(内存泄漏)时处理(取消链接)事件和回调可能是有意义的。我也在寻找一个模式。Prototype.js简化了这些方法的定义:
    var-Derived=Class.create(Base,{dispose:function($super){$super();…subclass dispose code…;})。但这不是自动的。你的问题很难回答,因为当你想做的是糟糕的功能本身时,你会说“有好的模式吗…”。没有其他语言隐式调用其父函数是有原因的。您需要显式地调用它。Albiet有一些方便的包装器,可以帮助您更轻松地调用派生的.prototype.foo=function(){base();console.log(“派生的foo”);}是的,我知道没有析构函数功能。我试图找到的是,是否有一个好的模式来模拟类似析构函数的东西。特别是,析构函数会自动调用所有的基本析构函数,所以我要寻找一个在JavaScript中(对于任何函数)执行相同操作的好模式。
    
        derived foo
        base foo
        one more thing in the derived function