How Do You Make a Class in JavaScript?
Problem
How do you create a class using either ECMAScript 5 or ECMAScript 6?
Solution
JavaScript is a prototype-based language, therefore its use of inheritance is also prototype-based. ECMAScript 5 uses functions to make classes. ECMAScript 6 introduces the class keyword as syntactical sugar for class creation in JavaScript.
The Code
Listing 17-1. Creating an ES5 Class and an ES6 Class
//ECMAScript 5 class
var Human = (
function Human(name){
this.name = name;
}
)
Human.prototype.sayGoodNight ...