in Frontend Tech, Programming

Late inheritance with __proto__ in javascript

In JavaScript there are so many different ways to inherit a super class. For example, Object.create, jQuery.extend, Child.prototype = new Parent(), and so on.

However, it’s not common to use __proto__ to create inheritance chain in javascript. Let me get to it right now.

So that does not look quite impressive. One cool thing about __proto__ is that any instance gets it when created. In javascript pretty much anything can be overwritten, which means after you create a regular object you can still make the instance inherit from a particular Class. Here’s a good example of late inheritance.

It sorta worked, but how about parent Class’ constructor execution? Unfortunately you will have to run one line of code as a late inheritance penalty.

That was a technique used by many old actionscript developers back in the days to create inheritance chain… We were cautious about its usage since it was undocumented feature and it could have been changed without telling anyone. I’m not sure if all browsers support that syntax, but that’s something good to know and understand how javascript works in my opinion anyways.

Comments are closed.