我在 javascript 中创建了这段代码:
function Shape() {}
Shape.prototype.name = "Shape";
Shape.prototype.toString = function() {
result = [];
if(this.constructor.uber) {
result[result.length] = this.constructor.uber.toString();
}
result[result.length] = this.name;
return result.join(', ');
}
function twoDShape() {};
twoDShape.prototype = new Shape();
twoDShape.prototype.constructor = twoDShape;
twoDShape.uber = twoDShape.prototype;
twoDShape.name = "twoD Shape";
var a = new twoDShape();
console.log(a.toString());
我不知道为什么,但是当我运行它时,firefox 就冻结了。我已经尝试了几个小时来解决这个问题。我的猜测是我的代码中应该存在无限循环,并且它位于 if 条件中的某个位置,但我没有找到它。有人可以帮我摆脱这个头痛吗?
谢谢你!
你打电话时this.constructor.uber.toString()
from Shape.prototype.toString
, uber
is twoDShape.prototype
这是一个Shape
,所以toString
方法是Shape.prototype.toString
again.
这会导致无限循环。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)