This is the third in a series of posts where I explore all the new stuff es6 has to offer, come along and learn about classes.
ECMAScript 6 introduces the class syntax, I suppose the first thing we would need to figure out is what does it replace or it’s equivalent in es5, so.
See the Pen ES6 Sampler- Classes pt1 by Eugenio - Keno - Leon (@k3no) on CodePen.
In a nutshell, if you already know about prototypes you alaready know how a class works, it is basically syntactical sugar (a more readable or convenient way) to deal with objects and inheritance.
This being a programming language, there is nunance and variation when defining a class, so..
See the Pen ES6 Sampler- Classes pt2 by Eugenio - Keno - Leon (@k3no) on CodePen.
Class Methods
A lot of what makes an object useful is the internal machinery as I like to call it or Methods as most people do, let’s figure out how to make some methods with es6 classes:
See the Pen ES6 Sampler- Classes pt3 by Eugenio - Keno - Leon (@k3no) on CodePen.
Extending Classes
Another handy thing one can do with classes is extending them so they add or change behaviour from a base class (or prototype) , let’s try that:
See the Pen ES6 Sampler- Classes pt4 by Eugenio - Keno - Leon (@k3no) on CodePen.
Calling super
One last things we must investigate, is how does one refer to the original class within an extended class?
See the Pen ES6 Sampler- Classes pt5 by Eugenio - Keno - Leon (@k3no) on CodePen.
And that’s it, a quick overview of what classes bring to Javascript; One last thing I would like to start doing, is leaving a final challenge, one you might encounter on a coding test, so let’s try that:
See the Pen ES6 Sampler- Classes pt6 by Eugenio - Keno - Leon (@k3no) on CodePen.
Classes in es6 while being syntactic sugar, are a welcome adition if you considered protoype chaining and inheritance a bit cumbersome, what’s more, it aligns javascript with other languages that use classes.
Classes in es6 = delicious prototype/inheritance sugar
Best,
Keno.