OOP in JavaScript
Because of the popularity of JavaScript in web development, it is used mainly in a functional way. This led many developers to the assumption that there is no way to do OOP in JavaScript. Even before the release of the ES6 standard, there was a way to define a class: by using functions. You might have seen this way of defining a class before in legacy frontend code. For example, if you want to create a class called Food
, you would have to write something like this:
function Food(name) { this.name = name; } var leek = new Food("leek"); console.log(leek.name); // Outputs "leek"
After the release of ES6, an increasing number of developers adopted the modern way of writing JavaScript classes using the class
keyword. In this chapter, we will be going over ways to declare classes using the ES6 standard.
Defining a Class in JavaScript
Before we dive into the latest syntax to define a class in JavaScript, let's go over...