I want to present the third part of the series "ES6 for beginners" created by one of our devs, Bartosz. You can find the previous parts here.
Exactly as I mentioned. Our class is a function by which we will later create objects.
There are still a few things missing in our Dog class. The first one will be a Constructor. This is a special keyword used to initialize the object data. A Constructor can accept any number of parameters. In our example, we will create two (name and height). Then, using variable this, we will ensure that the arguments passed in the parameters will be saved in the variables. In our case, also with the same names. Of course, it is possible to name the classy property differently than the parameter, as in the example below, however, it is not very legible and not recommended.
The name of our object in this example will be Staffie. Next, we pass to parameters that we defined in the constructor two arguments. The first "Bur" will be our name, the second 60 will be the height.
Also, we can create methods in classes. The method is just a function that is placed inside the class. So let's add a method that will return the sound of barking.
Since we know how to create classes, let's look at the inheritance. Since the dogs are divided not only by races but also groups, let's create a Terrier class that will inherit from the Dog class. It is also not complicated.
In the above example, we see that to be able to inherit from another class, we use the word extends + the name of the class from which we want to inherit. Besides, we see keyword super. It allows us using the special variable this and have access when creating an instance of the Terrier class to the name and height parameters which are inherited from Dog class.
Another thing that is often encountered is when we want to use our method barking in the case of Terriers. However, the returned value would be something different than in a regular dog.
Generally, convention from languages like Java in which there is an object-oriented programming model also assumes holding classes in different files. Namely, instead of just like in our example one file with two classes, a better solution would be to transfer them to two separate files. Thanks to such a procedure, our code would become much more readable which is a great advantage when writing.
As you can see we used two keywords. Import and Export. As you probably already figured out what they are used for. I will only mention that you can export functions, objects, or primitive values from the module so other programs with the import statement can use them.
Another topic worth mentioning in this part is the static methods. So what are they exactly? First of all, they are created with a keyword static. The most important thing about them is that they are called directly on the class and are not callable on an instance of the class. Their purpose is to create a utility function.
Great, it does.
In the next post we'll talk about rest & spread.