
2.1.1 Encapsulation
Example 1. Encapsulation. Grouping, Hiding, and Internal Data Control
It refers to the grouping of data and methods that operate on that data within the same unit, called a class. In addition, encapsulation involves hiding internal implementation details and exposing only what is necessary through a public interface.
2.1.2 Abstraction
2.1.2.1 Implementing Abstraction
Example 1. Abstract Class
This concept refers to the process of hiding complex implementation details and showing only the essential functionalities of an object. Abstraction allows programmers to focus on what an object should do without worrying about how it does it.
2.1.3 Inheritance
Example 1. Prototypal Inheritance in JavaScript
Every object in JavaScript has an internal link to another object called a prototype. When attempting to access a property or method on an object, JavaScript first looks in the object itself and then in its prototype, and so on until it reaches null.
Example 2. Inheritance in JavaScript using ES6 class syntax
Inheritance in JavaScript is a mechanism that allows a class (or constructor function) to inherit properties and methods from another class. This facilitates code reuse and the creation of more complex structures from simpler components. JavaScript implements inheritance mainly through the prototype chain and, more recently, through the class syntax introduced in ECMAScript 6 (ES6).
Example 3. Inheritance. Creating the Truck class and its derived child classes
Polymorphism is a fundamental concept in Object-Oriented Programming (OOP) that allows different objects to be treated as instances of the same base class, even if those objects belong to different derived classes. In JavaScript, polymorphism refers to the ability of a function or method to operate on different objects in a uniform way, as long as those objects share a common property or method.
Polymorphism allows the same operation to be executed in different ways depending on the object that invokes it. This is achieved through inheritance and method overriding in derived classes.
Example 1. Basic Example of Polymorphism in JavaScript
2.1.5 Types of Polymorphism in JavaScript
Although they do not strictly fit traditional categories, in JavaScript we can identify the following types of polymorphism.
2.1.5.1 Subtype Polymorphism (Prototypal Inheritance)
Example 1. Subtype Polymorphism
2.1.5.2 Parametric Polymorphism (Higher-Order Functions)
Example 1. Parametric Polymorphism
2.1.5.3 Ad Hoc Polymorphism (Duck Typing)
Example 1. Ad Hoc Polymorphism
2.1.5.4 Dynamic Polymorphism in Action
Example 1. A Restaurant
2.3 Objects and Constructors
Objects are a fundamental way of organizing and manipulating data. There are two main methods for creating objects: using object literals and constructor functions.
2.3.1 Object Creation.
2.3.1.1 Creating objects using object literals ({})
An object literal is defined using curly braces {} and specifying key-value pairs. This method is ideal for creating unique and simple objects.
Example 1. Creating an object using an object literal
2.3.1.2 Creating Objects using Constructor Functions
Constructor functions are useful for creating multiple objects with the same properties and methods. They are defined as regular functions but used with the new keyword.
Example 1. Definition of a constructor function