简体   繁体   English

为什么即使构造函数在其原型链中,类的instanceof仍返回false?

[英]Why does instanceof a class return false even though the constructor is in its prototype chain?

In a NodeJS app, I am trying to check that a value passed into a function is an instance of a specific Class, however I am getting unexpected behavior using instanceof between modules, and when checking the equality of the Class. 在NodeJS应用程序中,我试图检查传递到函数中的值是否是特定类的实例,但是在模块之间以及检查类的相等性时,我在使用instanceof时出现了意外行为。

endpoint.js endpoint.js

import SomeClass from 'utils/class';
import SomeModel from 'models/model';

const model = SomeModel.findOne({id: 'abc'});
const values = {a: 'b'};
const classInstance = new SomeClass({id: 'def'});

classInstance instanceof SomeClass //returns true
Object.getPrototypeOf(classInstance) //returns SomeClass {}

model.update(values, { a: classInstance, b: SomeClass });

When the classInstance is passed through to the update function, I see unexpected behavior. 当classInstance传递给更新函数时,我看到了意外的行为。

Calling Object.getPrototypeOf(a) returns SomeClass, as does a.constructor.name . 调用Object.getPrototypeOf(a)返回SomeClass, a.constructor.name也是a.constructor.name However, a instanceof SomeClass returns false. 但是, a instanceof SomeClass返回false。

Furthermore, just checking equality between the class imported and the class passed into the function returns false. 此外,仅检查导入的类和传递给函数的类之间的相等性将返回false。

require.resolve('utils/class') returns the same path for both imports. require.resolve('utils/class')为两个导入返回相同的路径。

models/model.js 模型/ model.js

import SomeClass from 'utils/class';

class Model {
  async update(values, injections) {
    const { a, b } = injections;

    // checking instance
    a.constructor.name //returns SomeClass
    Object.getPrototypeOf(a) //returns SomeClass {}
    a instanceof SomeClass; //returns false

    // checking class
    b === SomeClass; //returns false
  }
}

I would expect that b === SomeClass would return true, just like a instanceof SomeClass should also return true, unless I am missing something. 我希望b === SomeClass会返回true,就像a instanceof SomeClass也应该返回true一样,除非我丢失了某些东西。 Thanks for any help. 谢谢你的帮助。

utils/class.js utils / class.js

export default class SomeClass {
  constructor(foo) {
    this.bar = foo;
  }
}

Edit: The code is being transpiled with @std/esm. 编辑:正在使用@ std / esm编译代码。 NODE_PATH=./src/ nodemon -r @std/esm src/server.js

Is it due to the SomeClass instances being defined multiple times (due to transpiler, etc)? 是否由于多次定义SomeClass实例(由于编译器等)? Consider this code which will return false: 考虑以下代码,该代码将返回false:

(function() {

    class Foo {};

    class Bar {
        check(a) {
            console.log(a instanceof Foo);
        }
    };

    window.bar = new Bar();

})();

(function() {
    class Foo {};
    const foo = new Foo();
    window.bar.check(foo);
})();

vs Foo, Bar, etc., being defined in a global scope only 1 time ( require() should cache these dependencies and thus you shouldn't run into this behavior): VS Foo,Bar等,仅在全局范围内定义1次( require()应该缓存这些依赖项,因此您不应该遇到此行为):

class Foo {};

class Bar {
    check(a) {
        console.log(a instanceof Foo);
    }
};

const foo = new Foo();
Bar.prototype.check(foo);

There's an issue on the std/esm project where someone's experiencing this same thing. std / esm项目中存在一个问题 ,即有人正在经历同样的事情。 I don't use that library, so no ideas on the specifics. 我不使用该库,因此没有具体的想法。

Or I could be way off. 否则我可能会离开。

Charlie in the comments pointed us the right way. 查理在评论中指出了正确的方法。

https://github.com/DaveStein/esm-bug is reproducing this and I've noted it as such in the issue Charlie reported https://github.com/standard-things/esm/issues/633 . https://github.com/DaveStein/esm-bug正在重现此问题,我在Charlie报告的问题https://github.com/standard-things/esm/issues/633中就已经指出了这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM