Hey,
I understood that the class is a new syntax in new ES6 it is the alternative to the constructor function
this is the constructor function syntax
function User(name, age, salary) {
this.name = name;
this.age= age;
this.salary = salary;
}
and this is how to generate objects from the constructor function
let employee = new User("Hamza", 25, 1000);
and here is the new syntax in ES6 using the class which as I understood just a new syntax with no differences under the hood, it is also used to generate objects
class User {
constructor(name, age, salary){
this.name = name;
this.age= age;
this.salary = salary;
}
}
and to generate an object it is the same way as the constructor function
let employee = new User("Hamza", 25, 1000);
now when I get to learn new concepts like static properties and methods everything got a mess in my head how it is possible to add properties and methods out of the constructor isn’t the constructor that constructs the object so what about what is out of it
class User {
constructor(name, age, salary){
this.name = name;
this.age= age;
this.salary = salary;
}
static countMembers() {
return `${this.age} years old`;
}
}
Challenge: ES6 - Use class Syntax to Define a Constructor Function
Link to the challenge: