4 ms·
The question is whether that ~15 lines of code, written once, is worth the substantial increase in the surface area of the language, especially when overlapping
by jallmann 11y ago
The question is whether that ~15 lines of code, written once, is worth the substantial increase in the surface area of the language, especially when overlapping so much with the existing prototypal inheritance mechanism. The choice to use Java/C++-style inheritance is a design pattern that doesn't necessarily reflect the only (or even the best) way to accomplish things.
- qaq 11y agoIt's to a degree for people who are not primarily JS developers and often have no clue about prototypical inheritance. It also to a good degree reflects how V8 or some other engine actually treating your code under the hood.
- keithwhor 11y agoThe language surface area has already increased. The decision has been made.
- Touche 11y agoES6 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.
- yelnatz 11y agoBeing a C++ developer during the day and a web dev at night, this syntactic sugar is actually a breath of fresh air.
- jallmann 11y agoThe 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.
- Touche 11y agoYou can add object properties to a ES6 class's prototype, no? EDIT: Just tested in Firefox and indeed you can.
- jallmann 11y agoNot in the class declaration, though. Why force the programmer to go through the prototype, when ES6 provides such a "convenient" abstraction around it?
- phpnode 11y agoComing to the spec soonish and already supported in Babel - https://github.com/jeffmo/es-class-fields-and-static-properties https://github.com/jeffmo/es-class-fields-and-static-propert...
- djacobs 11y agoI'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).
- secoif 11y ago> 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/3af5a10c6b51f9e99d9f90394645d7ea630d5eaa/inherits_browser.js#L17 https://github.com/isaacs/inherits/blob/3af5a10c6b51f9e99d9f...
- djacobs 11y agoThanks 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.