I have an instance from Square which inherits from Rectangle
instance instanceof Rectangle --> true
instance instanceof Square --> true
instance.area() ; // --> area is defined by Rectangle
Now, in my code I don't know where the 'area' function is defined and I want the prototype object which defines it. Of course I can traverse the prototype chain (not tested)
var proto = instance ;
while( !(proto = Object.getPrototypeOf(proto)).hasOwnProperty('area') ) {}
// do something with 'proto'
However, I was wondering if there is a better/faster way to get the prototype object to which a function belongs ?
No. There isn't. You have to traverse the prototype chain:
Now you simply do:
If
instance
or its prototypes do not have the propertyarea
thenowner
returnsnull
.