Classes

Another way to create a blueprint for object that will be born later is to define a class. This relatively new (ES2015 way) method requires the class keyword followed by the name of the class.

Then we do exactly what we did before but this time inside a constructor function. This is exactly as before, and this is the function that will be executed any time an object uses this class alongside with the 'new' keyword in order to be created. Nothing changes (at least for now) and we can pass the arguments in order to populate our objects with different values inside the same properties.

Take a loop at the previous example below, this has exactly the same result as before.

Then why should someone bother with this syntax?

Classes

Kostas Diakogiannis
Module by Kostas Diakogiannis, updated more than 1 year ago
No tags specified