How can I test if an JavaScript object is an implementation of an interface using the Google Closure inheritance mechanism?
I could not find any hint of my.Animal
in the objects created via new my.Dog()
and object instanceof my.Animal
didn't work. The only information about the interface are compiler errors when forgetting to implement methods in the child class.
/**
* @interface
*/
my.Animal = function() {};
/**
* Does something.
* @return {string}
*/
my.Animal.prototype.doSomething;
/**
* @constructor
* @implements {my.Animal}
*/
my.Dog = function() {};
/** @inheritDoc */
my.Dog.prototype.doSomething() = function {
return "something";
}
var dog = new my.Dog();
console.log(dog instanceof my.Animal); // returns false
One way I found is to approximately test for the property of the interfaces, though that's bad in so many aspects:
console.log(!!dog.doSomething); // returns true