javascript继承中的堆栈溢出
我有下面的程序
Object.prototype.inherit = function(baseConstructor) {
this.prototype = (baseConstructor.prototype);
this.prototype.constructor = this;
};
Object.prototype.method = function(name, func) {
this.prototype[name] = func;
};
function StrangeArray(){}
StrangeArray.inherit(Array);
StrangeArray.method("push", function(value) {
Array.prototype.push.call(this, value);
});
var strange = new StrangeArray();
strange.push(4);
alert(strange);
,当我运行它时,我得到堆栈溢出? 为什么?
I have the below prog
Object.prototype.inherit = function(baseConstructor) {
this.prototype = (baseConstructor.prototype);
this.prototype.constructor = this;
};
Object.prototype.method = function(name, func) {
this.prototype[name] = func;
};
function StrangeArray(){}
StrangeArray.inherit(Array);
StrangeArray.method("push", function(value) {
Array.prototype.push.call(this, value);
});
var strange = new StrangeArray();
strange.push(4);
alert(strange);
and when irun it i get stack overflow? why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将
StrangeArray.prototype
设置为Array.prototype
。 稍后,您将向StrangeArray.prototype
添加一个push
方法(现在与Array.prototype
相同)。 所以你实际上是在设置Array.prototype.push
。 您的push
方法调用Array.prototype.push
- 即它本身。 所以它最终只是重复地调用自己,然后就会出现堆栈溢出。You're setting
StrangeArray.prototype
toArray.prototype
. Later, you're adding apush
method toStrangeArray.prototype
(which is now the same thing asArray.prototype
). So you're effectively settingArray.prototype.push
. Yourpush
method callsArray.prototype.push
- i.e. itself. So it ends up just calling itself repeatedly and you get a stack overflow.乔恩是对的。 这是修复它的方法。 这将允许您将 StrangeArray.prototype 设置为 Array 的新实例,而不是将 StrangeArray.prototype 设置为 Array.prototype,因此它继承了 Array.prototype 的属性(无需调用 Array 的构造函数)。
编辑:
在此示例中有效,但在更复杂的程序中效果不佳。 如果基本构造函数执行一些初始化,例如创建 DOM 元素、递增计数或连接到服务器,则所有初始化都将在脚本加载时发生,而不是在实例化子对象时发生。
处理这个问题的另一种方法是在构造函数中区分它是被调用用于继承(分配给原型)还是只是为了实例化,如果是被调用用于继承,则不执行初始化操作。 但我更喜欢在继承时不调用构造函数,因此在继承时为构造函数使用空函数。
我不太擅长解释这些东西。 我推荐 Crockford 的网站以及两篇文章 Javascript 继承。
Jon is right. Here's a way to fix it. Instead of setting StrangeArray.prototype to Array.prototype, this will let you set StrangeArray.prototype to a new instance of Array, so it inherits Array.prototype's properties (without calling Array's constructor).
Edit:
works in this example, but is not good in more complex programs. If the base constructor does some initialization, such as creating DOM elements, incrementing a count, or connecting to a server, then all that initialization would take place when the script loads, instead of when the child object is instantiated.
Another way to deal with this is to differentiate in the constructor whether it is being called for inheritance (to assign to a prototype) or just to instantiate, and then not do the initialization stuff if it is being called for inheritance. But I prefer to just not call the constructor when inheriting, and so use an empty function for the constructor when inheriting.
I'm not very good at explaining this stuff. I recommend Crockford's site and also these two articles on Javascript inheritance.
实际上,您只需将实际对象的原型设置为 baseConstructor 的新实例:
我还建议您查看一下 原型继承技术非常干净。
You actually only have to set the actual object's prototype, to a new instance of the baseConstructor:
I also suggest you to give a look to this Prototypal Inheritance technique that I found very clean.