OOP AND PROTOTYPE IN JAVASCRIPT
Object-oriented programming Is a paradigm that focuses on objects instead of functions. Let’s look at the image below.
WHAT ARE THE OBJECTS?
Objects are data structures that integrate variables and functions within them. Variables are called properties and functions are called methods. The four pillars of OOP are shown in the image opposite.
ENCAPSULATION
To encapsulate means to enclose in a capsule. Related data and functions are grouped together. The functions or methods work internally with the data to perform the required operations.
ASTRACTION
The concept of abstraction is based on the “what.” the goal of abstraction is to expose the essentials and hide the complexities. Take a DVD player as an example, such an electronic device exposes a user interface consisting of the start, stop, pause etc. buttons. But it hides the hardware complexities behind it.
INHERITANCE
Through inheritance, an object can access properties and methods of another object avoiding duplication of code.
POLYMORPHISM
Polymorphism literally means having many forms. A same method named m can have different behaviors depending on the context in which it operates.
I briefly summarize with the image below.
ACCESS TO THE PROPERTIES OF AN OBJECT
As we know to access object properties we use dot notations, objectname.propertyname. Let us look at the code below to clarify the concept.
LITERAL OBJECT TO CONSTRUCTION FUNCTION
Sometimes to avoid unnecessary duplication of code you can use what are called factory functions or rather constructor functions. Look at the code given below.
HIDE DETAILS AND EXPOSE ONLY THE ESSENTIALS
We see how in JS one can simply implement one of the four pillars of OOP, the abstraction. Code below.
PROTOTYPE CHAIN
The mechanism in JS of inheritance is implemented through the prototype chain. Through it an object can access properties and methods of another object. With this mechanism we avoid duplication of code, obj2 and obj3 don’t need to define the method fn1() unless they need to redefine it. Look at the two figures below.
CODE EXAMPLE
Let’s look at an example on a prototype chain.
CHAIN OF PROTOTYPES REPORTED IN THE CODE
THE __PROTO__ PROPERTY
Using __proto__ on an object we can trace back to its prototype. By adding a whole series of instructions __proto__ we can move up the prototype chain. It is deprecated, to know the prototype of an object use getPrototypeOf().
SAMPLE CODE
SETPROTOTYPEOF AND GETPROTOTYPEOF
THE KEYWORD THIS
this is the reference to the current execution contex,which is the object in which we are operating. The first thing the JS engine does before the execution of the first line of code is to create a global execution context i.e., a global object in which all our code is executed, and it causes this be a reference to this global object (window). The value of this within a function depends on how it is invoked.
EXPLICIT BINDING WITH BIND, CALL, AND APPLY
Through explicit binding, it is possible to make this refer to a certain object. Call and Apply work almost the same way, on the other hand Bind takes as input the object to which this it needs to refer to and resituates a reference to the function.