重新实现了 Constructor[Symbol.hasInstance] 但它仍然不会被调用

Reimplemented Constructor[Symbol.hasInstance] but it still won't be called

所以,我正在为 Constructor[Symbol.hasInstance] 编写一些实现另一个函数的示例代码,我注意到我的新实现不会被调用。

下面的脚本是我所期望的:

function Pirate(name) {
    this.name = name;
}

const jackSparrow = {
    isPirate: true
};

// Notice how `jackSparrow` is not yet considered an instance of the `Pirate` object
console.log(jackSparrow instanceof Pirate); // false

// Now let's assign another function for `Pirate[Symbol.hasInstance]`
Pirate[Symbol.hasInstance] = function (anObj) {
    return anObj.isPirate;
};

// This will cause Pirate[Symbol.hasInstance] to be called with `jackSparrow`
console.log(jackSparrow instanceof Pirate); // true

我尝试向我的 Pirate[Symbol.hasInstance] 实现添加一个 console.log 调用,但它不会在控制台记录任何内容。

有人知道发生了什么事吗?为什么我的实现没有被调用?

我在 Node 6.9.1 上运行。

你会找到答案

Object.getOwnPropertyDescriptor( Function.prototype, Symbol.hasInstance).writable

它 returns false: 你不能用赋值 = 运算符写入函数的 Symbol.hasInstance 属性。 属性 永远不会被设置,所以它永远不会被调用。 (默默地失败对我来说感觉像是无益的行为,但你走了。如果你在严格模式下,一个 TypeError 会抛出一条有用的消息,这是你应该一直使用它的众多原因之一。)你可以仅在具有 Object.defineProperty.

的函数上定义 Symbol.hasInstance 属性
Object.defineProperty(Pirate, Symbol.hasInstance, {
    value: function(anObj) {
        console.log('Is he a pirate?');
        return anObj.isPirate;
    }
});

现在 jackSparrow instanceof Pirate 首先记录问题,然后 returns true.

说明了原因。如果对象已经将 属性 继承为不可写,则赋值不会定义 属性。

如果您不想使用明确的 属性 定义,请考虑使用 class 语法:

class Pirate {
  constructor(name) {
    this.name = name;
  }
  static [Symbol.hasInstance](anObj) {
    return anObj.isPirate;
  }
}
const jackSparrow = {
  isPirate: true
};
console.log(jackSparrow instanceof Pirate); // true