5 ms·
I’m quite unfamiliar with OOP in JavaScript, so I’d appreciate if someone could chime in with how this differs from ES5: is this just syntactic sugar, or is the
by saagarjha 8y ago
I’m quite unfamiliar with OOP in JavaScript, so I’d appreciate if someone could chime in with how this differs from ES5: is this just syntactic sugar, or is there a semantic change as well?
- grahamperich 8y agoIt's just syntactic sugar. Still using prototypal inheritance under the hood.
- chrismorgan 8y agoIt’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.)
- runarberg 8y agoFrom MDN[1]: > JavaScript classes, introduced in ECMAScript 2015, are primarily syntactical sugar over JavaScript's existing prototype-based inheritance. The class syntax does not introduce a new object-oriented inheritance model to JavaScript. [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- runarberg 8y agoDo note though that there is a lot of syntax that is hidden with the `class` keyword. So even though it is primarily a syntactic sugar, it is by no means trivial.
- ronilan 8y agoCalling something “syntactical sugar” makes it sound unhealthy. I found the ES6 classes to be very helpful in conceptualizing stuff. When I started playing with them I’ve built myself a demo https://codepen.io/ronilan/pen/PObzew https://codepen.io/ronilan/pen/PObzew as a clone of same done previously in Ruby. Then I found classes to be very useful for bigger “vanilla” projects (https://github.com/ronilan/BlockLike https://github.com/ronilan/BlockLike) I’d call them “syntactical vitamins”.
- labster 8y agoThere's no alliteration there. I'd go with syntactic soylent.
- krapp 8y ago> Calling something “syntactical sugar” makes it sound unhealthy. I would argue that, more often than not, it is, and it becomes less healthy the more of it you have. Javascript, for all its faults, is syntactically a simple language and it should have been kept that way.