Javascript 继承错误行为
我这里有一些 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
I have some js code here link deleted
If you open your js console and you'll run this code snipet
var r = new TempPopupForm("xxx");
r.create();
an error will appear
TypeError: this.init is not a function
this error is saying there is no init method implemented on this object.
But that's not true.As you can see in the code below the init method is declared.
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);
};
I guess something is wrong with the inheritance definition,but I can not figure out what it is.
BTW There are some third party dependencies.
EDIT
I was following this article and where the funcs are ordered like I have. The order actually works on the other classes, but not on this one.
http://www.kevlindev.com/tutorials/javascript/inheritance/inheritance10.htm
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要重新排序您的函数和实例化。由于您的构造函数使用其自己的原型方法之一进行初始化,因此它们都需要位于实例化对象的块之上。 JavaScript 提升顶级函数。
试试这个 -
You need to re-order your functions and instantiation. Since your constructor is using one of its own prototyped method to init, they both need to be above the block where you instantiate the object. JavaScript hoists top-level functions.
Try this -