Class in Javascript is syntactic sugar on top of prototypes. Class makes it easier to structure our code, and to read and maintain it.
Let’s see a simple class.
|
|
Class is the skeleton and provides the framework for an object. Object is an instance of the class and contains the structure supplied by class and the data.
Let us make the above code usable -
|
|
First we instantiate quiz1
object. Then we start calling the functions supplied by the class to track quiz points. At the very end, we can get the results and display them.
The same can be done by a function as well, but what if you have multiple quizzes being taken by multiple students.
We can simply do -
|
|
The data owned by the two objects is different, but the logic is the same.
Classes had made the biggest impact in the way I code in the past, and I am quite excited with the continued improvements to class features in Javascript.