r/javascript • u/runvnc • Oct 06 '15
Why ES6 Has Classes
https://github.com/runvnc/whyes6hasclasses3
u/Baryn Oct 06 '15
The answer is that classes and inheritance are core aspects of JavaScript development and there is a standard way to do them in ES6 using the
classandextendskeywords.
-1
u/tencircles Oct 06 '15
Classes do not exist in javascript. Inheritance was already taken care of years ago in a standardized (and more importantly prototypal) way with
Object.create. Classes are completely superfluous.2
u/Baryn Oct 06 '15
I like the syntax a lot more. Granted, I'm not sure what my JavaScript will look like in 6 years, but that's part of the fun.
2
u/benihana react, node Oct 06 '15
So here we have probably the two most popular JavaScript systems in the world in Node.js and React
wat
1
1
u/ishmal Oct 06 '15
Classes don't provide much additional functionality. What they do provide, if you decide to use them, is discipline on how you structure your code. And there are many programmers who could use all the help they can get per that topic.
1
u/FennNaten Oct 06 '15
I think the "why" is easier to explain, and without entering sterile debate.
Javascript earned a lot of popularity and is growing steadily on every platform. There are more and more developers using it. Javascript is designed by committee. Classes represent a highly demanded feature. Introduction of classes doesn't remove existing abilities and supported paradigms, pleases a lot of people (and can even drive in the field some reluctants), and the most important point: they offer a standard. Because hey, class-oriented inheritance-loving developers have been hacking their own implementations of extend and super, more or less efficient, for ages. And this would never go away without a standard. It was more likely to increase.
So committee decided to please a big user base, and, while at it, come up with a consistent implementation. ES2015 classes have a nice terse syntax, remain usable as expressions, remain hackable at runtime, will be used as support for ES7 decorators. Whatever my position on classes usage, I think those are as nicely implemented to fit the language as it was possible.
So IMHO, there are only advantages. Sure, some people won't understand how it works under the hood, won't dive into FP and prototypes, and will come up with big inheritance monsters. But would those people have dived in anyway, even without classes? I don't think so. They would have thrown some class framework in their code and call it a day. They're already doing it. At least they won't have this excuse anymore to introduce another third-party library or give birth to their own hack.
The fact that you can chose your own paradigm is something that I find really fundamental in javascript as it is now. And I appreciate that, as a general-purpose language, js tries to offer tools for each supported paradigm. (I would appreciate having a bit more functional tools though, come on, enough has been done on the OOP side for now! ;) )
0
u/clessg full-stack CSS9 engineer Oct 06 '15 edited Oct 06 '15
inheriting from a React component
Not something you want to do.
By the way, if you're going to invoke React, then the reason they were excited about ES6 classes is not because they love classes. It's because it provides a more standard, clean syntax than their own createClass.
The engineers at Facebook in the React team are broadly against classes, and support a more functional style. In fact, they just hired Dan Abramov, who has been very prominent in bringing functional programming paradigms to the JavaScript community.
They just added stateless component functions and plan to add more things like that, which will quickly become the standard way to write components. Much simpler than class-based components and much easier to test. Also prevents people from inheriting from React components, which is a horrible idea.
Edit: By the way, just because some popular framework uses some technique doesn't make that technique a good idea. For example, pointing out two instances of monkey-patching in popular frameworks doesn't mean we should all use monkey-patching and that it's obviously a good idea because [appeal to authority]. As it is, generic functions/objects are used much more often in Node and React than classes.
0
u/dwighthouse Oct 06 '15
No, ES6 has classes because Java programmers wanted classes. They wanted classes because they had a hard time implementing their ProblemFactories in JS syntax.
Post a strawman, I raise you a strawman of my own.
2
u/x-skeww Oct 06 '15
You think frameworks like Backbone were written by Java programmers? Everyone who used CoffeeScript is a Java programmer?
Java isn't the only language with classes. Also don't forget that JavaScript is supposed to look like Java. That's how it was made.
1
u/dwighthouse Oct 06 '15
If you start your comment off with "You think...", you're probably wrong or at least out of proportion.
I'm well aware of Javascript's history. I am well aware of many languages that use classes, including Java, having programmed in them for years.
My point is not so much that it is Java programmers who want this change, as it is that the writer of this article assigned a strawman as the reasons I (and others) disapprove of the slow 'classification' of JS. So, rather than give the actual, legitimate arguments (which this author has ignored completely), I simply responded in kind. If you want to know the actual reasons I oppose it, read through some of my past comments on reddit. I've enumerated them many times.
1
u/x-skeww Oct 06 '15
ES6 has classes because everyone and their dog was using some kind of classes and inheritance. It's the JavaScript programmers who apparently wanted this.
If your point isn't that Java programmers wanted this change, you should perhaps say something else. Kinda straightforward, really.
1
u/dwighthouse Oct 07 '15
So you've never once, under any circumstance, responded sarcastically to someone? You only ever say exactly the literal contents of your thoughts with no embelishments? You may just be more serious than I usually am.
2
u/x-skeww Oct 07 '15
https://en.wikipedia.org/wiki/Poe's_law
Sarcasm doesn't really work on the internet. There is no intonation, no gestures, no facial expressions, no anything. There is no way to distinguish a sarcastic remark from the real thing, if it lacks an excessive amount of hyperbole.
So, yes, I generally try to be very clear and specific, because getting the point across is already hard enough as it is.
1
u/dwighthouse Oct 07 '15
So I guess you missed the part where I clearly labeled my statement a strawman, then?
0
u/x-skeww Oct 07 '15
Straw man arguments are still used to argue in the desired direction.
It isn't the same as sarcasm.
1
u/dwighthouse Oct 07 '15
We're clearly talking right past each over. Have a nice day.
0
u/x-skeww Oct 08 '15
"Sarcastic" straw man:
"ES6 has classes because Java programmers wanted classes."
Sincere opinion:
"[...] the constant pushes to make Javascript more comfortable for Java programmers makes no sense."
Looks perfectly aligned to me.
→ More replies (0)0
Oct 06 '15
No, it is not supposed to look like java. It only has the name for marketing reasons, and thats it.
1
u/x-skeww Oct 06 '15
Java was a direct influence. Constructor functions were added to make it look more like Java.
And yes, it was even renamed to JavaScript for marketing reasons.
It was made to look similar to Java and it was marketed at Java developers.
0
Oct 08 '15
A special type of stawman because ES6 class 'syntax' only difference is in syntactic sugar.
0
u/papers_ Oct 06 '15
Jesus christ this argument with classes is as dumb as the US's war on drugs. By this I mean the ones who are fighting for classes and those who are not.
0
Oct 06 '15
[deleted]
2
u/ruinercollector Oct 06 '15
Nobody hates classes by themselves; they hate classical inheritance, which is the inheritance model ES6 supports.
"Classes" in ES6 are sugar around the "constructor" function feature that you already had in ES5. It's the same thing that was already present, but cleaned up and made a bit more clear. The whole new/constructor thing is an unnecessary feature, and I'd agree that object.create is better, but I'll take the new syntax over the old any day.
Object.create > Class Syntax > Constructor Syntax
In any case, it's never classical inheritance. It's prototypical inheritance. You still have a prototype chain, you can still modify that chain, etc.
class Point { constructor (x, y) { this.x = x; this.y = y; } } class SomethingElse { sayHi() { console.log('Hello World!'); } } var p = new Point(10, 15); Point.prototype.getArea = function () { return this.x * this.y; }; console.log(p.getArea()); p.__proto__ = SomethingElse.prototype; p.sayHi(); console.log(p.x); console.log(p.y);0
u/runvnc Oct 06 '15
inheritance via duck-typing and depency injection via prototypes,
Specifically what syntax do you propose for the cases of inheriting from a React component or EventEmitter?
0
u/PitaJ Oct 06 '15
“JavaScript inheritance patterns” @P1taJ https://medium.com/@PitaJ/javascript-inheritance-patterns-179d8f6c143c
Check methods 3 and 4. They show inheritance without the use of class.
6
u/konbit Oct 06 '15
The "Class" haters tend to be proponents of structuring code using plain objects or using factory functions. I personally don't mind that, but also I like just using simple constructors and prototypes, works for me and I've never had issues because of it. The new "Class" syntax of javascript is mostly a more straightforward way of implementing this, and it also proposes a standard way of modularizing your code. Right now javascript is a gong show when it comes to "best practices" of code modularization, and this can be super painful for devs wanting to share code but adhering to different paradigms. I think in the end, if people stop being stubborn, having this normalized syntax will HUGELY help the collaborative effort happening in the JS world.