ECMAScript 6, also known as ES6 is version 6 of the ECMA Script programming language. It provides new features for improving the language and writing cleaner code. Some aspects of ES6 are convention such as the variables let and const, while features like arrow functions have been widely adopted but aren't always the norm. Other parts of ES6 are less explored but are worth diving into.
A very brief definition of object-oriented programming (OOP) is that it is programming using self-contained code to create applications. TechTarget.com describes OOP as "a programming language model that organizes software design around data, or objects, rather than functions and logic."
In other languages, the recipe for creating similar objects is called a class. “In object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods).”
In other words, a class is a data type that must be defined before an object is created.
ES6 class is an object constructor function that uses the keyword class to declare the function and the constructor keyword used inside the function to add a new property. Methods are also included in the code body and do not use the function keyword.
We use the ES6 Class instantiation patter much like the pseudoclassical pattern. Let's take a look at how we would create an ES6 class.
The class keyword creates a new function that we name Pet. It's convention to use capital letters for constructor function declarations. Inside of that function is our constructor. We have two properties name and nickname and an eat method. Later on when we want to create a new instance of our object, we use the ‘new’ keyword.
Here we set myPet to a new instance of Pet passing it a name and nickname argument. We can call eat passing in trash as an argument and receive the expected console log value back.
ES6 Classes were introduced as an instantiation pattern for constructor functions and made to look and work like a class that is found in other programming languages. While not required, using ES6 features make for clean and readable code. It also allows you to use other enhancements only available when using ES6.