Javascript 继承 - instanceof 不工作?

标签 javascript oop inheritance instanceof

我正在使用 javascript 和 html5 编写一个简单的平台游戏。我以面向对象的方式使用 javascript。为了让继承工作,我使用了以下内容;

// http://www.sitepoint.com/blogs/2006/01/17/javascript-inheritance/
function copyPrototype(descendant, parent) {
    var sConstructor = parent.toString();
    var aMatch = sConstructor.match(/\s*function (.*)\(/);
    if (aMatch != null) { descendant.prototype[aMatch[1]] = parent; }
    for (var m in parent.prototype) {
        descendant.prototype[m] = parent.prototype[m];
    }
};

为了这篇文章,请考虑以下示例;

function A() {
 this.Name = 'Class A'
}
A.prototype.PrintName = function () {
 alert(this.Name);
}

function B() {
 this.A();
}
copyPrototype(B, A);

function C() {
 this.B();
}
copyPrototype(C, B);

var instC = new C();

if (instC instanceof A)
  alert ('horray!');

据我了解,我希望看到一个 horray 警告框,因为 C 是 C & B & A 的一个实例。我错了吗?还是我只是使用了错误的方法来检查?还是 copyPrototype 破坏了 instanceof 运算符?

一如既往地感谢您花时间阅读本文!

肖。

最佳答案

问题是 copyPrototype函数仅将属性从构造函数原型(prototype)复制到另一个构造函数原型(prototype),例如,最后,内部 [[Prototype]] C.prototype 的链接简单地指向 Object.prototype .

instC的原型(prototype)链构造函数的原型(prototype)如下所示:

                [[Prototype]]
    A.prototype -------------->|-------------------|
                               |                   |
    B.prototype -------------->|  Object.prototype | ---> null
                               |                   |
    C.prototype -------------->|-------------------|
        ^
        |
      instC

The instanceof operator traverses the prototype chain, your instC object, as you can see, will have on its prototype chain only C.prototype and Object.prototype.

You can achieve what you want by setting your constructor's prototypes to be object instances of their "parent" constructors, for example:

function A() {
  this.Name = 'Class A'
}

A.prototype.PrintName = function () {
  alert(this.Name);
}

function B() {
  //..
}
B.prototype = new A();
B.prototype.constructor = B; // fix constructor property


function C() {
  //..
}

C.prototype = new B();
C.prototype.constructor = C; // fix constructor property

var instC = new C();
if (instC instanceof A)
  alert('horray!');

现在instC的原型(prototype)链对象看起来像这样:

           ---------------        ---------------        ---------------
 instC --> | C.prototype | -----> | B.prototype | -----> | A.prototype |
           ---------------        ---------------        ---------------
                                                                |
                                                                V
                                                       --------------------
                                                       | Object.prototype |
                                                       --------------------
                                                                |
                                                                V
                                                               null

推荐文章:

关于Javascript 继承 - instanceof 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3145700/

相关文章:

javascript - Sencha Ext JS 4,使用另一个面板创建可拖动面板时遇到问题

javascript - 如何使用 JavaScript 读取 JSON 文件

oop - 工厂模式是否违反了 "Tell, Don' t Ask”原则?

java - 更新类的Jpanel

Ruby——嵌套类和子类是一回事吗?

c# - 自动调用基方法

javascript - 为什么除了 `goog.base(this)` 之外还需要 `goog.inherits()` ?

javascript - jQuery AJAX setInterval 有时无法点击

C++ 错误 : invalid use of 'AppleFarmer::AppleFarmer' when ca

javascript - 将变量作为 URL 参数传递到 xmlHTTPRequest