Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inherit STATIC methods?

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?

like image 562
silent_coder Avatar asked Oct 16 '13 18:10

silent_coder


1 Answers

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;
like image 81
SLaks Avatar answered Sep 29 '22 00:09

SLaks