In previous section, I'd introduce a little bit about object via prototype.
The principles of OOP are:
This section will introduce how to achieve these by using javascript. Polymorphism and encapsulation is not cover in this section because it's
There is no term of abstraction in javascript. But just remember in OOP, what is abtraction, then we can simulate to create an our own abstract class - of course, it's not really abtract but we can put our logic to make it abstract in our logic in program.
In another word, this means creating a class extends from another class. Unfortunately, there is no "extend" in Javascript.
Remember the section Scope. We can change context of a function call by invoke it with bind(), apply(), call().
So, imagine that we have function B - as base class and function A - the one we want to extends from B. So if we call constructor of B from A with context A , A will have everything of B. It sounds like similar inheritance, right?
Example:
Essentially, the pattern often used in javascript is defining high-order-class function with properties and encapsulate functionalities by prototype.
Prototype is shared object between instances of an function.