Prototype js 允许通过 $super 调用 super 方法。我需要从对象调用类方法,但在重写方法中,如下所示:
var ClassA = Class.Create({
initialize: function(options) {
Object.extend(this, options);
},
method1: function(){/*some code*/}
});
var ClassB = Class.Create(ClassA, {
method1: function($super) {
$super(); // this works fine, calls ClassA.method1()
}
});
var objectA = new ClassA({
method1: function($super) { // I need something like this
$super(); // this not works, must calls ClassA.method1()
}
});
我该怎么做?
最佳答案
如果您只想 objectA
拥有新方法,请执行以下操作:
var objectA = new ClassA();
objectA.method1 = objectA.method1.wrap(function (fn) {
fn(); // works like a `$super` call inside of a class
});
它使用 Function#wrap围绕函数添加建议。
如果您需要向函数传递参数,请执行以下操作:
var objectA = new ClassA();
objectA.method1 = objectA.method1.wrap(function () {
var args = $A(arguments), fn = args.shift();
fn(args); // works like a `$super` call inside of a class
});
关于javascript - 原型(prototype) js super 方法调用或类似的东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23865887/