JavaScript - Classes
JavaScript - Classes
JavaScript Classes
The JavaScriptclassesare a blueprint or template for object creation. They encapsulate the data and functions to manipulate that data. We can create the object using classes. Creating an object from a class is referred to as instantiating the class. In JavaScript, the classes are built on prototypes. The classes are introduced to JavaScript in ECMAScript 6 (ES6) in 2009.
For example, you can think about writing code to represent the car entity. A code can contain the class having car properties. For different cars, you can create an instance of the class and initialize the car properties according to the car model.
Before ES6, the constructor function was used to define a blueprint of the object. You can define the constructor function as shown below.
function Car(brand) { // Constructor function
this.brand = brand; // property initialization
}
const carObj = new Car("Audi"); // Creating an objectDefining JavaScript Classes
The syntax of the class is very similar to the constructor function, but it uses the βclassβ keyword to define the class. As we can define a function using function declaration or function expression, the classes are also can be defined using class declaration or class expression.
Syntax
The syntax of class definition in JavaScript is as follows β
// class declaration
class ClassName {
// Class body
}
//Class expression
const ClassName = class {
// class body
}A βClassNameβ is a class name in the above syntax.
A JavaScript class is a function, but you canβt use it as a regular function.
Type of JavaScript Classes
A JavaScript class is a type offunction. In the example below, we used the βtypeofβ operator to get the type of the class. It returns the βfunction, which you can observe in the output.
Output
The type of the car class is: functionThe constructor() method
When you use the function as an object blueprint, you can initialize the object properties inside the function body. Similarly, you need to use theconstructor()method with the class to initialize the object properties.
Whenever you create an instance of the class, it automatically invokes theconstructor()method of the class.
In below example, we use the constructor() method to create aCarclass β
class Car {
constructor(brand) {// Defining the constructor
this.brand = brand;
}
}The constructor() method has no specific name but can be created using the βconstructorβ keyword. You can initialize the class properties using the βthisβ keyword inside the constructor function.
Creating JavaScript Objects
To create an object of a JavaScript class, we usenewoperator followed by the class name and a pair of parentheses. We can pass thee arguments to it also.
Letβs create an object calledmyCaras follows β
const myCar = new Car("Audi");Thethiskeyword inside the constructor function refers to an object that is executing the current function.
Example: Creating class objects without arguments
In the example below, we have defined the βCarβ class. The class contains the constructor and initializes the properties with default values.
After that, we have created the instance of the class, and you can observe it in the output.
Output
Car brand is : BMW
Car model is : X5
Car year is : 2019If you want to initialize the class properties dynamically, you can use the parameters with the constructor() method.
Example: Creating class objects with arguments
In the example below, we have defined the βCarβ class. The constructor() method of the class takes 4 parameters and initializes the class properties with parametric values.
While creating the βCarβ class instance, we passed 4 arguments. Inthisway, you can initialize the class properties dynamically.
Output
Car brand : BMW
Car model : X5
Car price : 9800000
Car year : 2019JavaScript Class Methods
You can also define the methods inside the class, which can be accessed using the class instance.
Syntax
Follow the syntax below to define methods inside the class.
class car {
methodName(params) {
// Method body
}
}
obj.methodName();In the above syntax, βmethodNameβ is a dynamic name of the method. To define a class method, you donβt need to write any keyword like βfunctionβ before the method name.
To invoke the class method, you need to use the instance of the class. Here, βobjβ is an instance of the class. You can also pass the parameters to the method.
Example
The example below demonstrates how to pass parameters to the class methods.
Here, we have defined theupdateprice()method to update the price of the car. So, while invoking theupdateprice()method, we pass the new price as an argument and use it inside the method body to update the price.
You can see the original and updated price of the car in the output.
Output
The car price is : 9800000
After updating the car price is : 8800000JavaScript Class Hoisting
In JavaScript, the declaration of the class is not hoisted at the top of the code. So, you always need to define the class before you use it.
const carObj = new Car(); // This will generate an error.
class Car {
}You can try to run the above code. It will generate a reference error as the car class is used before its initialization.
Strict Mode with Classes
The strict mode is used to avoid unusual errors. The class code is always in the strict mode by default.
Letβs understand it via the example below.
class numbers {
constructor() {
num = 90; // Defining variable without var keyword
}
}
const numObj = new numbers();In the above code, we define the βnumβ global variable in the constructor() method. In the strict mode of JavaScript, it is not allowed to define the variables without using the var, let, or const keywords. So, the above code will throw an error.