在JavaScript中调用重写的方法

Ole*_*egM 2 javascript oop superclass

我试图找到一种方法在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]);
Run Code Online (Sandbox Code Playgroud)

你觉得怎么样,可以吗?或者它可能会产生"内存泄漏"(参考自己的原型)?非常感谢


谢谢你的回复,我尝试了你的代码.但是如果我是SubClass的子类,对于examle 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();
Run Code Online (Sandbox Code Playgroud)

谢谢

Arn*_*anc 6

您可以使用属性来存储类的父级,并直接在父类上调用方法:

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);
};
Run Code Online (Sandbox Code Playgroud)

你可以添加一点抽象:

// 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);
};
Run Code Online (Sandbox Code Playgroud)

你可以像这样使用它:

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

    // calling super.foo();
    // Pass the calling class as first parameter
    this.super(SubClass, 'foo');
};    
Run Code Online (Sandbox Code Playgroud)

在这里试试:http://jsfiddle.net/ZWZP6/2/

在CoffeeScript中

在咖啡脚本中,您可以使用super()[docs]构造:

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

class SubClass extends BaseClass
    foo: ->
        console.log('called SubClass.foo')
        super()
Run Code Online (Sandbox Code Playgroud)

  • 我在http://jsfiddle.net/ZWZP6/2/上更新了答案和示例代码; 这适用于多个级别的继承 (2认同)
  • 我想你刚刚在 CoffeeScript 上卖掉了我 (2认同)