This is a question about the new(ish) class
keyword in JavaScript.
I understand how classes work in other languages such as PHP, C++ and Java, and I also understand how JavaScript objects work.
The key distinguishing feature is that JavaScript uses the prototype for all of the hard work, and, among other things, it makes it easy to retrofit properties.
There is clearly a syntactical difference between the traditional JavaScript way of creating constructor functions & prototypes, and the newer class
syntax.
As far as I can see, the newer syntax also adds a few additional features such as static properties, and also doesn’t hoist, since it is no longer defined in a function.
The question is:
Is creating a class
going to give me the same results as using the traditional method? That is, do I still have the prototype mechanism and other traditional features?
I ask this because I would like to know whether it is safe to move to the newer syntax without having to change everything else. If class
is, as I suspect, syntactic sugar, then it will be easy to transition.