我在JavaScript创建的代码块: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条件中存在,但我没有发现它。有人能帮助我摆脱这种头痛。 谢谢!
为了准确,按照我上面的回答,它在toString方法中循环。构造函数是相同的,这就是为什么会发生这种情况(方法调用自己),但实际上并不是循环本身的原因。 – sync