Creating and managing classes
Classes were introduced in ES6. They are syntactic sugar over the prototype-based inheritance. Historically, JavaScript did not have formal classes as we can expect from the typical Object Oriented Programing (OOP) languages.
In this section, we will learn how to create classes and how to use them with ES6. Also, we will explore how the prototypical inheritance is a key feature in maintaining retro compatibility and extends JavaScript’s core features.
Creating a class
To create a class, we need to use the class
keyword, and then we can define the default properties of the class using the constructor
method:
class Human{ constructor(name, age) { this.name = name; this.age = age; } } const jane = new Human ("Jane", 30); console.log(jane.name); // Jane console.log(jane.age); // 30
In this example, we created a class called Human
and then we created an instance...