0

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.

Manngo
  • 14,066
  • 10
  • 88
  • 110
  • Yes, `class` still creates a constructor function with a `.prototype` and static properties on the function itself. – Bergi Dec 06 '17 at 08:51
  • The mozilla docs might be of help: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes –  Dec 06 '17 at 08:52
  • @Bergi so `class` is just syntactic sugar? – Manngo Dec 06 '17 at 08:52
  • @jeff Actually, it was that article which led me to ask this question. One thing it doesn’t mention is whether it is really something different or just a different way of doing it. – Manngo Dec 06 '17 at 08:54
  • @Bergi Yes, I agree that it’s a duplicate. Neither the search engine nor SO managed to find the duplicate question. Thanks – Manngo Dec 06 '17 at 08:57
  • 1
    @jeff I was wrong. I should learn to read the first paragraph … – Manngo Dec 06 '17 at 08:59

0 Answers0