typescript MyObject.instanceOf() typescript MyObject.instanceOf() typescript typescript

typescript MyObject.instanceOf()


If you are willing to accept the prototypal nature of JavaScript you can just use instanceof which checks the prototype chain:

class Foo{}class Bar extends Foo{}class Bas{}var bar = new Bar();console.log(bar instanceof Bar); // trueconsole.log(bar instanceof Foo); // trueconsole.log(bar instanceof Object); // trueconsole.log(bar instanceof Bas); // false


You can do it.Just replace your instanceOf implementation with this one:

public instanceOf(cls: { CLASS_NAME: string; }) {    return cls.CLASS_NAME === this.className || super.instanceOf(cls);}


I guess this might work Example on Playground

var getName = function(obj) : string {    var funcNameRegex = /function (.{1,})\(/;   var results = (funcNameRegex).exec(obj);   return (results && results.length > 1) ? results[1] : "";};class Foo{}function IsTypeOf(obj: any, type: Function){    alert(obj.constructor.name + " == " + getName(type));}IsTypeOf(new Foo(), Foo);

Related