I have a class
function Man(){...}
Man.drinkBeer = function(){...}
I need to inherit SuperMan from Man. And I still want my Superman be able to drink some beer.
How can I do that?
Object.setPrototypeOf(SuperMan, Man);
This will set the internal __proto__ property of your derived function to be the base function.
Therefore, the derived function will inherit all properties from the base function.
Note that this affects the functions themselves, not their prototypes.
Yes, it's confusing.
No existing browser supports setPrototypeOf(); instead, you can use the non-standard (but working) alternative:
SuperMan.__proto__ = Man;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With