JavaScript - How to create(imitate) a class?
Creating a class using function constructor. This is a conventional approach used in JavaScript to imitate classes without using class keyword. Older version of JS was not having class keyword.
let Person = (function() {
function Person(name, title = 'Mr.') {
//defining properties
this.name = name;
this.title = title;
}//defining functions
.prototype.greet = function() {
Personconsole.log(`Hello ${this.title} ${this.name}`);
;
}return Person;
;
}())
let manoj = new Person('Manoj');
.greet(); manoj
Creating a class using class keyword.
class Person {
constructor(name, title='Mr.'){
this.name = name;
this.title = title;
}greet() {
console.log(`Hello ${this.title} ${this.name}`);
}
}let manoj = new Person('Manoj');
.greet(); manoj
Comments
Post a Comment