在JavaScript中调用重写的方法 [英] Calling overridden methods in JavaScript

查看:123
本文介绍了在JavaScript中调用重写的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种方法在JS中调用超类的重写方法并得到它。

i tried to find a way to call overridden method of superclass in JS and got this.

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]);

您怎么看?可以吗?
或者它可能会产生内存泄漏(参考自己的原型)?
非常感谢

What do You think, is it ok ? Or maybe it can produce 'memory leaks' (reference to own prototype)? Thanks a lot

感谢您的回复,
我尝试了您的代码。
但是如果我是SubClass的子类,对于examle var a = SubClass1(),
(SubClass1有自己的foo())

Thank You for your reply, i tried Your code out . But if I subclass SubClass, for examle var a= SubClass1(), ( SubClass1 has its own foo())

并调用a .foo(),那么只会调用SubClass1和BaseClass foo,

and call a.foo(), then only SubClass1 and BaseClass foo's will be called,

不是SubClass foo()
代码:

not SubClass foo() Code:

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();

谢谢

推荐答案

您可以使用属性直接在父类上存储类'parent和调用方法:

You can use a property to store the class' parent and call methods directly on the parent class:

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/

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

In coffee script you can use the super()[docs] construct:

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

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

这篇关于在JavaScript中调用重写的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆