4 ms·
I think ES6 classes are the worst of all worlds. I know people from a Java background really want classes in their Javascript but this is not that. ES6 classes
by ChrisSD 8y ago
I think ES6 classes are the worst of all worlds. I know people from a Java background really want classes in their Javascript but this is not that.
ES6 classes look like kind of sort of like a class but if you poke it at all you see lots of sharp edges and weird behaviours that make no sense unless you know what it's sugar for.
- gedy 8y agoI think the point was to simplify the JS Prototype syntax to be more "normal-looking" and less confusing to devs, not some rich OOP model compared to true OOP languages.
- ChrisSD 8y agoJavascript is a true OOP language. I'm all in favour of simplifying some of the syntax but why call it a "class"? That's only going to further confuse matters. I already have a hard enough time trying to teach Javascript's OOP model without adding to the confusion.
- kowdermeister 8y agoWhy not? It's totally reasonable if you learn a language to get familiar with the concepts in it. Every language is different here and there and the 'class' describes well what it does: wrap things together that belong together.
- krapp 8y ago>and the 'class' describes well what it does: wrap things together that belong together. So does "Object". All "Class" does, here is paper over some of the complexity of making JS objects. If anything, JS classes hide necessary concepts about the language from the developer. At least with the old, ugly way of doing things, you were forced to confront prototypes and scope and how 'this' worked.
- dmitriid 8y ago> I think the point was to simplify the JS Prototype syntax to be more "normal-looking" and less confusing to devs So they bolted on an incompatible thing that hides prototypes and is confusing to devs: class C { y = 10; // this fine. "methods" defined as class properties // have access to proper `this` due to how class properties work // and scoping rules x = () => if(this.y > 10) this.y = 10; constructor() { // if you don't bind `this`, `method()` will not have access // to `this` this.method = this.method.bind(this) } method() { // will refer to a wrong `y` if `this` isn't bound // in the constructor this.y++; } }
- gedy 8y agoSure, but this is being addressed in: https://github.com/tc39/proposal-class-fields https://github.com/tc39/proposal-class-fields And available now as: https://babeljs.io/docs/en/babel-plugin-proposal-class-properties https://babeljs.io/docs/en/babel-plugin-proposal-class-prope...
- dmitriid 8y agoNo, it's not addressed. And you can see me using class fields already in `x = () => ...` <- this is a class field (I called it class property, sorry). The fact that arrow functions declared as class fields get access to the proper `this` in the class instance is just a happy(?) coincidence with its own downsides. For example, class fields can't be inherited in the child class. So this adds a yet another layer of confusion for devs. And as you see in the example at the link you provided, you still need to `.bind(this)` the actual methods.
- oklahoma 8y agoYou don't have to bind 'method' in the constructor to access 'y'. Just put 'this.y = 10;' in the constructor. Your example isn't legal JavaScript.
- dmitriid 8y agoIt's perfectly legal Javascript. Why would I put this.y = 10 in the cinstructor? Do you even understand what this code does? const instance = new C(); for(let i = 0; i < 20; i++) { instance.method(); } instance.x() If you don't bind `this` to `method` in the constructor, when you call `instance.method()`, `this` will be whatever (window, IIRC). Don't believe me? Try a more complex example like React's event handling with `this.setState()`: https://reactjs.org/docs/handling-events.html https://reactjs.org/docs/handling-events.html
- Waterluvian 8y agoI think ES6 classes were one of the best things added in ES6. Before then there were a half dozen idiomatic ways to make "classes". Now there's one main way. It's just syntactic sugar. They're not real classes, whatever real means. But they do their job just fine.
- nielsbot 8y agoThey're real enough? Smells like a class, tastes like a class... I think it's unfortunate that classes now have more syntax conveniences than that for building prototypal object hierarchies. I find `Object.create(...)` and `Object.assign(...)` unclear to read and clunky. As another aside: is there any reason to bother with JS constructors and "functional" OOP? I just use `Object.[create|assign](...)`, done.
- kowdermeister 8y agoIt's "javascript classes" why should it 1:1 copy Java or other languages? Just for the sake of people coming from other language background I don't see it justified. Do people complain about differences between C++, Java, C# classes?
- ChrisSD 8y agoBecause it uses a fundamentally different model to C++, Java and C# classes?
- kowdermeister 8y agoBut it's also used for kinda the same thing. Besides the naming I find it a very welcome addition to the language.
- ww520 8y agoWhat do you like class in JavaScript to be?