Showing posts with label Inheritance. Show all posts
Showing posts with label Inheritance. Show all posts

Javascript Inheritance Part 4: Putting it All Together

This is part 4 of a four part series on Javascript inheritance.
Part 1: Overview and Use Case
Part 2: Delegation
Part 3: Concatenation
Part 4: Putting it All Together

For this part of the series I will no refer to class-based inheritance as 'class inheritance' and prototypal inheritance as 'object composition'.

Final Terminology Clarification

There are several programming paradigms or ways of thinking about how to break down a problem so that your program can solve. Javascript is multi-paradigm, but one of the ways you can choose to structure your program is object-oriented, whose goal is to break a problem down into objects that interact with each other. In general, there are two styles of approaching how to determine the relationship between these objects: class inheritance and object composition. Another way to call these is class-based inheritance and prototype-based inheritance respectively. Or to use adjectives, classical inheritance and prototypal inheritance. Yes, this is a source of confusion. Many choose to refrain from using inheritance to refer to object composition.

Javascript Inheritance Part 3: Concatenation

This is part 3 of a four part series on Javascript inheritance.
Part 1: Overview and Use Case
Part 2: Delegation
Part 3: Concatenation
Part 4: Putting it All Together

Concatenation Implementation

Concatenation works by copying the methods from a prototypal to the object that wants to inherit without reference to the prototypal. Delegation works by copying just a reference to the prototypals via a call stack, but concatenation copies the methods over and attaches them to the object directly. Concentation has the following properties:

Javascript Inheritance Part 2: Delegation

This is part 2 of a four part series on Javascript inheritance.
Part 1: Overview and Use Case
Part 2: Delegation
Part 3: Concatenation
Part 4: Putting it All Together

Prototypal Inheritance

There are two types of prototype-based inheritance: delegation (aka. differential) and concatenation (aka. cloning). And because Javascript allows for a lot of freedom, there are several design patterns to implement either type of prototypal inheritance, each with its own name. This combination of different inheritance paradigms, delegation vs concatenation, and implementations in combination with Javascript being the first to bring many of these concepts to the mainstream is what makes the terminology so confusing.

Javascript Inheritance Part 1: Overview and Use Case

Despite having programmed in Javascript for several years now, and I never knew until recently that the type of object-orientated programming that it follows is prototype-based and not class-based. And indeed I did not know even know object-orientated programming was not synonymous with classes. When researching the subject, I found most material didn't explain clearly, and thus this is aimed to remedy that. It is aimed at those with an understanding of closure in Javascript, a tutorial for which you can find here.

Because Javascript is very grassroots in its development, the terminology surrounding these concepts is a tad unclear. The goal of this series is to clarify. If you are already familiar with the concepts and just want a top-down overview, feel free to skip to part 4.