Hacker News new | ask | show | jobs
by grahamperich 2716 days ago
It's just syntactic sugar. Still using prototypal inheritance under the hood.
1 comments

It’s not quite only sugar, though it’s close, just as spread is almost purely sugar, but when applied to constructors can do things that were beforetimes impossible.

My favourite subtle distinction is that methods are not constructors—this is true of object shorthand as well.

You were taught that these two were equivalent?

  var x = { foo() {} };
  var x = { foo: function() {} };
Try this on each:

  new x.foo
The second definition works, but the first fails because x.foo is not a constructor. This has actually caught me, in a codebase with an old-style Class() creator (and no, `class` is not quite a suitable replacement there, at this stage, for reasons I won’t go into). `Class({ init: function(){} })` would work fine, but `Class({ init() {} })` would yield an uninstantiable class—except that the result was being fed through Bublé, which reinstated the `: function` for old browsers’ sake. So it was only when I removed Bublé from the pipeline that it blew up!

(To demonstrate this same point in ES6 classes, `new (new class { foo() {} }).foo` will fail.)