Why Class is different in javascript?

What is a class? Why use a class? Class Instantiation Class Expressions Class declaration

What is a class?

It is a template to make objects. Classes also serve as a blueprint(a standard) for creating objects, providing a way to organize and structure code. Classes allow us to define objects with similar properties and methods.

Classes were introduced in javascript in ES6(ecmascript 6). A class is defined by the class keyword, followed by the class name and a block of code that defines the properties and methods of the class.

Why use a class?

We use classes for various reasons. We can discuss some topics to know the uses of classes.

Object-Oriented Programming:

Classes are used to do object-oriented programming in javascript, which is a popular programming formal for organizing code into objects and classes.

Reusability

Classes are used to create various objects with similar properties and methods. You do not need to write code every time for every object.

Abstraction

Classes provide a way to abstract the implemention details of objects and hide the complexity of the code. It makes easy to understand the code.

Encapsulation

Classes provide a way to encapsulate data and behavior within objects, making it easier to manage state and behavior in a structured way.

Modularity

Classes provides a way to organize code into distinct, reusable units, making it easier to manage and maintain large codebases.

Overall, classes in javascript are useful tool for organinzing and structuring code, making it easier to write, understand, and maintain.

Class Instantiation

Creating an object from a class in javascript is refers to class instantiation. An object for a class is a kind of primitive data type that shows characteristics and behavior of a class. The 'new' operator is used to create an istance of the class and returns the object.

When 'new' keyword is used, it creates an oject in following steps:

  1. It creates a new, empty object.

  2. It sets the prototype of the newly created object to be the prototype property of the constructor function being invoked with the new keyword.

  3. It binds the 'this' keyword within the constructor function to the newly created object.

The 'this' keyword refers to the object that is currently executing the code. It can be used for object properties and object methods.

A class can be instantiated in two way declarations and expressions.

Let's see example of a class of a person for better understanding.

Class declaration

It is class declaration in which a class is created in this style.

class Person{

setName(name) { //method

this.name=name;//property

}

setAge(age) { //method

this.age=age; //property

}

introduction() {

console.log("Hi, my name is ",this.name", and i am",this.age, "years old");

}

}

creating new object

const myPerson=new Person();

set name of the new person.

myPerson.setName("Ankit"); // access method by dot notation.

Set age of the new Person.

myPerson.setAge(40); //access method by dot notation.

Introduce the person.

myPerson.introduction();

Class Expressions

Let's see the example below.

const myClass=class {

constructor(expression) {

this.result=expression;

}

};

const myInstance=new myClass(10+12);

console.log(myInstance.result);

In this example, the class expression is assigned to the constant myClass.

Did you find this article valuable?

Support WebIndia by becoming a sponsor. Any amount is appreciated!