我在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());
Run Code Online (Sandbox Code Playgroud)
我不知道为什么但是当我运行它时,firefox就冻结了.我一直在努力解决这个问题.我的猜测是我的代码中应该有一个无限循环,并且它存在于if条件的某个地方,但我没有找到它.有人可以帮我解决这个问题.谢谢!
当您this.constructor.uber.toString()从调用时Shape.prototype.toString,uberistwoDShape.prototype是 a Shape,因此该toString方法Shape.prototype.toString又是。
这会导致无限循环。