javascript - 当我调用 this.constructor.superclass.someMethod 时,超出最大调用堆栈大小会引发异常

标签 javascript inheritance prototype

我正在学习 JavaScript。
我发现了流行的扩展功能:

function extend(Child, Parent) {
    var F = function() { }
    F.prototype = Parent.prototype
    Child.prototype = new F()
    Child.prototype.constructor = Child
    Child.superclass = Parent.prototype
}

让我们创建 3 个类

function foo() {}
foo.prototype.identify = function() {
    return "I'm a foo";
}

function bar() {}
extend(bar, foo)
bar.prototype.identify = function() {
    return "I'm a bar and " +
        this.constructor.superclass.identify.apply(this, arguments);
}

function zot() {}
extend(zot, bar)
zot.prototype.identify = function() {
    return "I'm a zot and " +
        this.constructor.superclass.identify.apply(this, arguments);
}

因此我们有以下继承方案:

foo->bar->zot

让我们编写一些代码:

f = new foo();

alert(f.identify()); // "I'm a foo"

b = new bar();

alert(b.identify()); // "I'm a bar and I'm a foo"

z = new zot();

alert(z.identify()); // stack overflow

丢失的行产生

Uncaught RangeError: Maximum call stack size exceeded(…)

您能详细解释一下发生了什么吗?

最佳答案

因为一旦 identify.apply(this) 被调用两次(就像在 zot 中,与 bar 不同),this 将指向两个函数上的同一个实例,并将无休止地调用自己的 identify() 方法。

相反,尝试显式指向基本 identify 方法:

function extend(Child, Parent) {
  var F = function() {}
  F.prototype = Parent.prototype
  Child.prototype = new F()
  Child.prototype.constructor = Child
  Child.superclass = Parent.prototype
}

function foo() {}
foo.prototype.identify = function() {
  return "I'm a foo";
}

function bar() {}
extend(bar, foo)
bar.prototype.identify = function() {
  return "I'm a bar and " +
    bar.superclass.identify.apply(this, arguments);
}

function zot() {}
extend(zot, bar)
zot.prototype.identify = function() {
  return "I'm a zot and " +
    zot.superclass.identify.apply(this, arguments);
}

var f = new foo();
alert(f.identify()); // "I'm a foo"

var b = new bar();
alert(b.identify()); // "I'm a bar and I'm a foo"

var z = new zot();
alert(z.identify()); // "I'm a zot and I'm a bar and I'm a foo"

参见Fiddle

关于javascript - 当我调用 this.constructor.superclass.someMethod 时,超出最大调用堆栈大小会引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40842246/

相关文章:

javascript - 在原型(prototype)的构造函数中初始化属性时获取 'undefined'

javascript - 您可以在原型(prototype)函数中访问原型(prototype)属性吗?

javascript - goog.provide() - 未捕获的语法错误 : Unexpected token ILLEGAL

javascript - 这些可见性设置适用于除 Safari 之外的所有浏览器。为什么?

java - 制作任何东西的通用参数化类型

python - 如何继承异常来创建更具体的错误?

java - 从继承自同一父类(super class)的多个类实现回调接口(interface)时发生冲突

Javascript 代码未被调用/无法正常工作

javascript - 在输入类型 "loading..."元素中选择文件后显示文本 ="file"

c - 将结构定义放在函数原型(prototype)之前,但结构需要来自 argv 的信息