JavaScript类:(此示例)

时间:2018-11-08 08:32:16

标签: javascript class object this instanceof

我要检查对象是否是当前类的实例 它可以从类外部正常运行,但是如果我从类内部调用它会给出错误

class test {

  check(obj) {
    return (obj instanceof this) //error: this is not a function

  }
}


const obj = new test()

console.log(obj instanceof test) //true
console.log(new test().check(obj)) //ERROR

解决:

方法1 :(通过:@CertainPerformance 我们不能使用:返回obj instanceof

因为(this)是一个对象(即OBJECT的obj instance),

所以我们可以使用承包商对象:

return obj instanceof this.constructor

方法2 :(作者:@Matías Fidemraizer

   return Object.getPrototypeOf(this).isPrototypeOf () //using this->better 

   //or: className.prototype.isPrototypeOf (obj) 
      //if you know the class name and there is no intent to change it later

方法3 :(作者:@Thomas) 使功能“检查”为静态

static check(obj) {
    // now `this` points to the right object, the class/object on which it is called,        
    return obj instanceof this;
  }

3 个答案:

答案 0 :(得分:2)

具体错误消息是:

  

未捕获的TypeError:“ instanceof”的右侧不可调用

在线

return (obj instanceof this)

这很有意义-instanceof的右侧应该是 class (或函数),例如test。不是函数的东西(如对象)无法调用,因此<something> instanceof <someobject>没有意义。

请尝试改为引用对象的构造器,该对象将指向类(test):

return obj instanceof this.constructor

class test{
  check(obj){
    return obj instanceof this.constructor

  }
}
obj=new test()
console.log(obj instanceof test) //true
console.log(new test().check(obj)) //ERROR

答案 1 :(得分:1)

instanceof用于测试某个给定原型的给定实例是否提供了构造函数。

实际上,您无法检查{} instanceof {},这是您的实际操作。

这是第一个支票有效而另一个不起作用的原因。

使用Object#isPrototypeOf怎么办?

class A {
   check (x) {
       return A.prototype.isPrototypeOf (x) 
   }
}

class B extends A {}

class C {}

const a = new A ()
const b = new B ()
const c = new C ()

console.log (a.check (a))
console.log (a.check (b))
console.log (a.check (c))

或者,就像@vlaz在某些评论中指出的那样,您可以使用Object.getPrototypeOfthis中提取原型:

class A {
   check (x) {
       return Object.getPrototypeOf(this).isPrototypeOf (x) 
   }
}

class B extends A {}

class C {}

const a = new A ()
const b = new B ()
const c = new C ()

console.log (a.check (a))
console.log (a.check (b))
console.log (a.check (c))

答案 2 :(得分:0)

imo。 check不属于当前位置。将这种功能作为实例方法编写是没有意义的。最好是静态方法:

class Test {
  // check is now a static method
  static check(obj) {
    // and now `this` points to the right object, the class/object on wich it is called, 
    // not some instance.
    return obj instanceof this;
  }
}

// extending the class Test
class Test2 extends Test {}

const obj = new Test(), obj2 = new Test2();
// Tests
[
  () => obj instanceof Test, //true
  () => Test.check(obj), //true
  () => new Test().check(obj), //ERROR: because `check` is no longer an instance method

  // Test2 has inherited static check:
  () => obj2 instanceof Test2, //true
  () => Test2.check(obj2), //true
  () => Test2.check(obj), //false, obj is no instance of Test2
  () => Test.check(obj2), //true
  () => {
    var check = Test.check;
    // ERROR, because here you call `check` with no class/object
    return check(obj);  
  }
].forEach(fn => {
  try {
    console.log("%s: %o", fn, fn());
  } catch (err) {
    console.error(err);
  }
});
.as-console-wrapper{top:0;max-height:100%!important}