OOP in JavaScript
One crucial programming paradigm is object-oriented programming, or OOP. Objects are the main organizing principle of this collection of strategies for structuring programming. The core concept of object-oriented programming (OOP) is straightforward and obvious: an object is a logically connected set of information and capabilities. This method produces code that is easier to maintain and reuse.
By combining similar attributes and functionality, working with OOP in JavaScript enables you to think about many topics in terms of objects. One way to think of objects is as a grouping of attributes and functions. As variables, properties can store basic data structures like texts and numbers or even other objects. The activities or functions that an object can carry out are called methods, and they contain code that is run when the method is called.
Classes
In JavaScript and object-oriented programming, classes are essential building blocks that improve application structure. A class acts as an object creation blueprint or template. The attributes and methods that each object formed from a class will have are essentially defined by the class. Although we may have already seen how to directly build objects, classes show us how to make a reusable template for a certain object type anytime it’s required. One method of designing modular, reusable code is to define classes.
The class keyword in JavaScript is used to define a class. Despite offering a useful syntax, JavaScript classes are a little different from those in other languages on the inside. They serve as a substitute syntax for constructor functions when defining objects. A class declaration, in technical terms, creates a function that acts as the constructor and saves its methods in the function’s prototype property.
Objects
An object instance, or just object in this sense, is a particular item made using the class blueprint. The new keyword will inherit the attributes and methods defined in the class when an object is created from it. A Car class might, for instance, define that every car object has four wheels and an engine. Each car you build would be an instance of that Car type.
Constructors
One unique method in a class is the Constructor. When the new keyword is used to create a new object instance, it is automatically run. Initializing the newly generated object instance’s properties is the constructor’s main goal.
Invoking a constructor function with new creates a new object automatically before the constructor’s logic executes, and this value can be accessed within the constructor. This object is then usually initialized by the constructor. Since the new keyword takes care of this automatically, constructors do not need to return the new object explicitly. Generally speaking, constructor function names and, by extension, class names start with a capital letter. This name scheme makes it easier for programmers to understand that when they use the new keyword, they should utilize it.
Functions that use the properties of the object are called methods in a class. You begin specifying these methods inside the class body with the method name rather than the function keyword.
Here is an example demonstrating a simple class with a constructor and a methodr:
class Person { // Defines a class named Person
constructor(firstName, lastName) { // The constructor method
// It is called automatically when a new object is created with ‘new’
// There can only be one constructor in a class
this.firstName = firstName; // ‘this’ refers to the object instance being created
this.lastName = lastName; // Properties hold the data of the class
}
greet() { // Defines a method named greet(). Methods are functions on a class
// Methods perform actions
console.log(`Hello, my name is ${this.firstName} ${this.lastName}.`); // Use ‘this’ to access instance properties
}
}
The class in this code is Person. Firstname and lastname are passed to the constructor as properties (this.firstname, this.lastname) to the object that is being built. One technique that returns the combined first and last names is fullname(). The new Person(…) syntax is used to generate the object instances person1 and person2, where new calls the constructor.