In JavaScript, Classes define blueprints for creating objects with similar properties and methods. They offer a more structured approach to object-oriented programming.
Class In JavaScript
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.
JavaScript
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);
|
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.
Javascript
class Person {
constructor(name, age) {
this .name = name;
this .age = age;
}
}
const person1 = new Person( "Alice" , 30);
console.log(person1.name);
console.log(person1.age);
const person2 = new Person( "Bob" , 25);
console.log(person2.name);
console.log(person2.age);
|
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.
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);
|
Output
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
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.
You can create a JavaScript class by using a predefined keyword named class before the class name.Â
Last Updated :
07 Mar, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...