How Make "typeof" On Extends In Javascript?
Example: class Foo extends Bar { } Foo typeof Bar //-> false :( How discover that Foo extend Bar?
Solution 1:
As ES6 classes inherit prototypically from each other, you can use isPrototypeOf
Bar.isPrototypeOf(Foo) // true
Alternatively just go with the usual instanceof
operator:
Foo.prototypeinstanceofBar// true// which is more or (in ES6) less equivalent toBar.prototype.isPrototypeOf(Foo.prototype)
Solution 2:
MDN for typeof
:
The typeof operator returns a string indicating the type of the unevaluated operand
you need instanceof
, isPrototypeOf
classBar{}
classFooextendsBar {}
var n = newFoo();
console.log(n instanceofBar); // trueconsole.log(Bar.isPrototypeOf(Foo)); // trueconsole.log(Foo.prototypeinstanceofBar); // true
Post a Comment for "How Make "typeof" On Extends In Javascript?"