Hacker News new | ask | show | jobs
by Touche 3799 days ago
ES6 classes do not use Java/C++ style inheritance, they are still the same as writing:

  function Foo() {

  }

  Foo.prototype = Object.create(Parent.prototype);

  ....
It's just sugar, nothing more.
3 comments

The semantics of JS classes are still different in enough subtle ways: constructors and constructor subclassing, (lack of) object properties, hoisting, etc. That is on top of the new keywords, new function syntax, etc. All that needs to be mapped onto the model of "classical" Javascript in order to completely understand the language. There are enough gotchas there to disqualify JS classes from being simply sugar.
You can add object properties to a ES6 class's prototype, no?

EDIT: Just tested in Firefox and indeed you can.

Not in the class declaration, though. Why force the programmer to go through the prototype, when ES6 provides such a "convenient" abstraction around it?
Coming to the spec soonish and already supported in Babel - https://github.com/jeffmo/es-class-fields-and-static-propert...
Being a C++ developer during the day and a web dev at night, this syntactic sugar is actually a breath of fresh air.
I'm not yet convinced that it's just syntactic sugar. For example, what (generic) code is `super` syntactic sugar for?

Even if it is just syntactic sugar, that syntactic sugar makes it much more approachable for devs who want to use classical inheritance (not that I'd encourage that).

> what (generic) code is `super` syntactic sugar for?

  super.propertyOnSuper ~= this.__proto__.propertyOnSuper

  super() in constructor ~= ParentConstructor.call(this)
and some tools provide such things like this.super_:

https://github.com/isaacs/inherits/blob/3af5a10c6b51f9e99d9f...

Thanks for the response. If the choice is between manually specifying that (type of) code for every kind of object (and likely moving to a #create factory method) or using a native syntax for class declaration, I can see why people would choose the latter.