簡體   English   中英

在JavaScript中調用覆蓋的方法

[英]Calling overridden methods in JavaScript

我試圖找到一種方法來調用JS中的超類的重寫方法,並得到了。

function A() {
    this.superclass = new Array(A.prototype);
}
A.prototype.call_method = function(method, args, pos) {
    if (!(pos >= 0)) pos = this.superclass.length - 1;
    while (!this.superclass[pos].hasOwnProperty(method)) if ((--pos) < 0) return;
    if (this.superclass[pos][method]) this.superclass[pos][method].apply(this, args);
    if (pos) this.call_method(method, args, pos - 1);
}
A.prototype.add = function() {
    this.superclass.push(arguments.callee.caller.prototype);
}
A.prototype.test = function(a, b, c) {
    alert("test of A ");
}

function B() {
    A.call(this);
    this.add();
}
B.prototype = new A();
B.prototype.constructor = B;
B.prototype.test1 = function(a, b, c) {
    alert("test of B ");
}

function C() {
    B.call(this);
    this.add();
}
C.prototype = new B();
C.prototype.constructor = C;
C.prototype.test = function(a, b, c) {
    alert("test of C");
}

var aa = new C();
aa.call_method("test", [1, 2, 3]);

您怎么看,可以嗎? 還是可能產生“內存泄漏”(參考自己的原型)? 非常感謝


謝謝您的答復,我嘗試了您的代碼。 但是如果我將SubClass子類化,例如var a = SubClass1(),(SubClass1有自己的foo())

並調用a.foo(),那么只有SubClass1和BaseClass foo會被調用,

不是SubClass foo()代碼:

function BaseClass() { } BaseClass.prototype.foo = function() {     
  alert('called BaseClass.foo'); 
};  
SubClass = function() { }; 
SubClass.prototype = new BaseClass; 
SubClass.prototype.foo = function() {     
    alert('called SubClass.foo');      
    // calling super.foo();   
    this.constructor.prototype.foo.call(this); 
}; 
SubClass1 = function() { }; 
SubClass1.prototype = new SubClass; 
SubClass1.prototype.foo = function() {     
    alert('called SubClass1.foo');      
    // calling super.foo();   
    this.constructor.prototype.foo.call(this); 
}; 
var a=new SubClass1();
a.foo();

謝謝

您可以使用屬性來存儲類的父類,並直接在父類上調用方法:

function BaseClass() {
}
BaseClass.prototype.foo = function() {
    console.log('called BaseClass.foo');
};

SubClass = function() {
};
SubClass.prototype = new BaseClass;
SubClass.prototype.__super__ = BaseClass;
SubClass.prototype.foo = function() {
    console.log('called SubClass.foo');

    // calling super.foo();
    this.__super__.prototype.foo.call(this);
};

您可以添加一些抽象:

// The `clazz` parameter allows super-classes to use super() too.
// It should be the calling class
BaseClass.prototype.super = function(clazz, functionName) {
    // take all the arguments after functionName
    var args = Array.prototype.slice.call(arguments, 2);
    // call the function on super's prototype
    clazz.prototype.__super__.prototype[functionName].apply(this, args);
};

您可以像這樣使用它:

SubClass.prototype.foo = function() {
    console.log('called SubClass.foo');

    // calling super.foo();
    // Pass the calling class as first parameter
    this.super(SubClass, 'foo');
};    

在這里嘗試: http : //jsfiddle.net/ZWZP6/2/

在CoffeeScript中

在coffee腳本中,您可以使用super() [docs]構造:

class BaseClass
    foo: -> console.log('called BaseClass.foo')

class SubClass extends BaseClass
    foo: ->
        console.log('called SubClass.foo')
        super()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM