OO
JavaScript provides functionality to describe classes via the class
keyword after ES6.
But these classes are just syntactic sugar, designed to emulate how classes behave. These are built on top of javascript prototype model.
Prototype Model
Every JS object has a hidden property called Prototype
(__proto__
in some environments).
This might point to another object or maybe null
.
Prototype Chain
This is kind of like an inheritance chain.
When we try to access a property on an object and its not found, JS checks the object's prototype and continues up the chain until the property is found or it reaches end of the chain and encounters null
.
This allows inheritance.
OOP#Inheritance
Implemented by extends
keyword
OOP#Encapsulation
JS enforces access control on members via prefixes. There are no special access modifiers as such.
- private:
#privateMember
- protected:
_protectedMember
- public:
publicMember
OOP#Polymorphism
Since, JS is dynamically types all types are determined at runtime. So polymorphism is also runtime. Its not possible to assign an object of child class to reference of parent class since there is no static typing. It is only at runtime that type of any object or reference is determined.
It does support OOP#Overriding parent class methods.