Skip to content Skip to sidebar Skip to footer

Javascript: Benefit Of Setting Subclass Prototype From A Function That Returns An Instance Of The Superclass?

I've been going back to basics with JavaScript principals lately and found a slight variation when extending a class that I am curious about. I know there are lots of options out t

Solution 1:

You're using the polyfil for Object.create

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FObject%2Fcreate

Child.prototype = Object.create (Parent.prototype);

Benefits are:

1: Parent constructor may need other complex objects to be passed that are not available when declaring Child.

2: Parent instance specific members are not on Child.prototype. Even though Parent.call(this... in Child constructor will shadow these members they still have no business being there.

More info here: Prototypical inheritance - writing up

Post a Comment for "Javascript: Benefit Of Setting Subclass Prototype From A Function That Returns An Instance Of The Superclass?"