ECMAScript 6 introduced the class keyword to create classes in JavaScript.
Now you can use the class attribute to create a class in JavaScript instead of a function constructor, and use the new operator to create an instance.
Let's create an Employee class:
class Employee {
constructor(firstName, lastName, emailId, age){
this.firstName = firstName;
this.lastName = lastName;
this.emailId = emailId;
this.age = age;
}
getFullName(){
return this.firstName + " " + this.lastName;
}
getFirstName(){
return this.firstName;
}
}
Now, we create an object of Employee class and access its methods:
const employee = new Employee('Ramesh', 'Fadatare', 'ramesh@gmail.com', 29);
console.log(employee.getFirstName());
console.log(employee.getFullName());
Comments
Post a Comment