In JavaScript, Classes define blueprints for creating objects with similar properties and methods. They offer a more structured approach to object-oriented programming.
Classes are similar to functions. Here, a class keyword is used instead of a function keyword. Unlike functions classes in JavaScript are not hoisted. The constructor method is used to initialize. The class name is user-defined.
Syntax:
class classname {
constructor(parameter) {
this.classname = parameter;
}
}
Constructor Method
The constructor method in JavaScript is a special method used for initializing objects created with a class.
It’s called automatically when a new instance of the class is created. It typically assigns initial values to object properties using parameters passed to it. This ensures objects are properly initialized upon creation.
Syntax:
class ClassName {
constructor() { ... }
method() { ... }
}
JavaScript Classes Examples:
Example 1: The below example illustrates the JavaScript classes.
class emp { constructor(name, age) {
this .name = name;
this .age = age;
}
} const emp1 = new emp( "Geek1" , "25 years" );
console.log(emp1.name); console.log(emp1.age); |
Geek1 25 years
Explanation: This JavaScript code defines a class `emp` representing employees with properties `name` and `age`. An instance `emp1` of this class is created with name “Geek1” and age “25 years”. Then, the `name` and `age` properties of `emp1` are logged to the console.
Example 2: This example demonstrates the use of constructor to create class in JavaScript.
class Person { constructor(name, age) {
this .name = name;
this .age = age;
}
} const person1 = new Person( "Alice" , 30);
console.log(person1.name); // Output: Alice
console.log(person1.age); // Output: 30
const person2 = new Person( "Bob" , 25);
console.log(person2.name); // Output: Bob
console.log(person2.age); // Output: 25
|
Alice 30 Bob 25
Explanation: Here, The Person
class has a constructor method that takes name
and age
parameters. When a new instance of Person
is created using the new
keyword, the constructor method initializes the name
and age
properties of the object.
Example 3: This example use class to create the object in JavaScript.
class emp { constructor(name, age) {
this .name = name;
this .age = age;
}
} const emp1 = new emp( "Geek1" , "25 years" );
const emp2 = new emp( "Geeks2" , "32 years" )
console.log(emp1.name + " : " + emp1.age);
console.log(emp2.name + " : " + emp2.age);
|
Geek1 : 25 years Geeks2 : 32 years
Explanation: This JavaScript code defines a class `emp` representing employees with properties `name` and `age`. Two instances `emp1` and `emp2` are created with different names and ages. Then, the properties of both instances (`name` and `age`) are logged to the console, showing different values for each employee.
JavaScript Classes Use-Cases
1. Classes and Objects in JavaScript
Classes in JavaScript provide blueprints for creating objects with similar properties and methods. Objects are instances of classes. Classes encapsulate data and behavior, promoting code reusability and organization. Objects created from classes can have unique data but share the same methods defined in the class blueprint.
2. How to create a JavaScript class in ES6 ?
You can create a JavaScript class by using a predefined keyword named class before the class name.