I am written the following code and able to accomplish same output using any of the two approaches. I am little confused and need to understand when should I create a class and when when should I create a function in NodeJs?
//Approach 1
class User {
constructor() {
this.name = "John";
this.age = 30;
}
getName() {
return this.name;
}
}
let user = new User();
console.log(user.getName());
//Approach 2
let userNew = function () {
this.name = "Mike";
this.age = 32;
this.getName = function () {
return this.name;
};
};
let user2 = new userNew();
console.log(user2.getName());