gpt4 book ai didi

javascript - 在 JavaScript 中调用重写的方法

转载 作者:行者123 更新时间:2023-11-29 16:24:39 25 4
gpt4 key购买 nike

我试图找到一种方法来调用 JS 中父类(super class)的重写方法并得到了这个。

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

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


谢谢您的回复,我试过你的代码。但是如果我将 SubClass 子类化,例如 var a= SubClass1(),( SubClass1 有它自己的 foo())

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

不是子类 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 中

在 CoffeeScript 中,您可以使用 super()[docs]构造:

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

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

关于javascript - 在 JavaScript 中调用重写的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7300552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com