JavaScript Classes
We've had classes in JS since 2015, BUT IT'S NOT A CLASS IN THE SENSE OF PHP OR OTHER LANGUAGES!
It's just a cleaner syntax for doing prototyping and inheritance
Creating a class
// we can create a class with the class syntax
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
let person1 = new Person('John', 20);
console.log(person1.name);
console.log(person1.age);
Inheritance
// we can also inherit from a class
class Employee extends Person {
constructor(name, age, job) {
super(name, age);
this.job = job;
}
}
Static methods
// we can also create a static method
class Animal {
crie() {
return this;
}
static mange() {
return this;
}
}
let obj = new Animal();
obj.crie(); // Animal {}
let crie = obj.crie;
crie(); // undefined
Animal.mange(); // class Animal
let mange = Animal.mange;
mange(); // undefined