我这里有一些js代码链接已删除
如果您打开 js 控制台,您将运行此代码片段
var r = new TempPopupForm("xxx");
r.create();
会出现错误
TypeError: this.init is not a function
此错误表示该对象上没有实现 init 方法。
但这不是真的。正如您在下面的代码中看到的,声明了 init 方法。
TempPopupForm.prototype = new PopupForm();
TempPopupForm.prototype.constructor = TempPopupForm;
TempPopupForm.superclass = PopupForm.prototype;
function TempPopupForm(name) {
this.init(name);
}
TempPopupForm.prototype.init = function(name) {
TempPopupForm.superclass.init.call(this, name);
};
我猜继承定义有问题,但我不知道它是什么。
顺便说一句,有一些第三方依赖项。
编辑
我正在关注这篇文章,其中的函数是像我一样排序的。该顺序实际上适用于其他类别,但不适用于此类别。 http://www.kevlindev.com/tutorials/javascript/inheritance/inheritance10.htm
最佳答案
您需要重新排序您的函数和实例化。由于您的构造函数使用其自己的原型(prototype)方法之一进行初始化,因此它们都需要位于实例化对象的 block 之上。 JavaScript 提升顶级函数。
试试这个 -
function TempPopupForm(name) {
this.init(name);
}
TempPopupForm.prototype = new PopupForm();
TempPopupForm.prototype.constructor = TempPopupForm;
TempPopupForm.superclass = PopupForm.prototype;
TempPopupForm.prototype.init = function(name) {
TempPopupForm.superclass.init.call(this, name);
};
var r = new TempPopupForm("xxx");
r.create();
关于Javascript 继承错误行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7822112/