48 points · by breck · 9y ago · 55 comments
That's true if you define the word "class" to not include JS classes, but in that case, you can use the keyword and still "embrace" prototypal inheritance, since as you say, it's all just objects (even if you use the class keyword).
Alternatively if you use a more flexible definition of the word class, then your assertion is not true, because JS does have classes, they just are implemented via prototypes.
Either way, I don't see how your comment is correct. Is it all objects, or not? :)
There is not a single fact proving that statement. People want to write Javascript in different ways, Javascript is no less OO that it is FP. I don't see why one camp should be more privileged than the other.
> prototypal inheritance you will write cleaner code with fewer errors.
JS Classes use "prototype inheritance". compare that :
function Foo(){}
function Bar(){
Foo.call(this)
}
Bar.prototype = Object.create(Foo.prototype)
with that : class Foo{}
class Bar extends Foo{}
There is absolutely no debate as to what is more readable, cleaner,easier to parse for IDE, and less error prone. ES6+ is considerably making writing JS easier and safer. There is very little reason today to deal with prototypes directly. And in strict mode, calling Foo or Bar without using new yields an error.Yes.
> Using composition with Object.assign is far less fragile than inheritance.
Absolutely not. Object.assign is an example of inheritance, not composition, and it's just as fragile as the others. Eric Elliott is well known for calling "composition" what everyone else calls "multiple inheritance". See, eg:
http://wsieroci.github.io/elliott/inheritance/composition/20...
https://www.reddit.com/r/javascript/comments/3oy9c3/composit...
It's almost a running joke on the /r/javascript subreddit at this point.
Furthermore, Javascript classes make writing mixins AKA composition a breeze, compared to before:
http://justinfagnani.com/2015/12/21/real-mixins-with-javascr...
This link is an excellent rebuttal to Eric Elliot article and how he achieve composition through copying by the way.
So even if you like composition over inheritance, Javascript classes still allow you to write safer and clearer and cleaner code. Now you can do away with ugly factories and property copying.
> Object.assign is far less fragile than inheritance
You realize the using Object.assign means copying properties from object to object over and over and increase memory requirements in order to run a script.
So my point still stands, there is not a single disadvantage introduced with the class keyword.
You can get rid of "this" and all it's problems, and the code is even cleaner.
[citation needed]
Classes are fine, they're just sugar over prototypal inheritance.
class A {}
> class A {}
class B extends A {}
> class B extends A {}
new B instanceof B
> true
new B instanceof A
> true
There is just 1 way to do this with classes, it is very concise, and it is impossible to do wrong, e.g., by forgetting to call the super class constructor. Try doing that with prototypes.Do you have a concrete example in mind? Code sample A is cleaner and has fewer errors than its class equivalent B?
This is what's wrong with the JS community and why everything is so hype driven... this list gives the impression that if you want to be a good JS developer you should've even THINK about trying to use classes in your JS because if you do then you don't truly "get" JS.
Think for yourself and decide if classes will improve your code in the way you plan to use it. Classes have hugely improved my company's JavaScript, it works well considering our developers understand OOP much better than they do functional programming, which I think is pretty common.
We don't rely on inheritance, we reuse by composition (which is just as easy to do with class as it is to do with protypes).
Classes are fine! Use them if you want to! Don't use them if your existing code relies heavily on prototypal inheritance, but if you just wanna wrap some methods around some internal state and you like the way the class syntax looks compared to the prototype syntax don't let some think piece on medium try to convince you that classes are bad.
EDIT: sorry to go off on a slightly OT rant on your comment... I just got frustrated by the link you posted.
He's asking about why manipulating prototypes directly is better than doing so via the class keyword sugar, and your response seems to explains why you probably shouldn't do either one.
- JS: https://github.com/cpojer/js-codemod/tree/master/transforms
- React: https://github.com/reactjs/react-codemod/tree/master/transfo...
We built and ran the codemod they talk about in this paper many years ago across thousands of files. Unfortunately, we didn't have the infra open sourced so the actual codemod didn't get open sourced.
>`Array.forEach` and `Object.keys` were new features in ES5, should people just leave their old `for` loops anyways?
Emphatic yes, people should keep using their "old" loops. They do less and are much faster.
The most surprising part is the title "... The Good, The Bad, and The Ugly" given that Crockford's similarly named Javascript book brings up relevant cases that I didn't see addressed.
People are using them all over the JS-verse already, just manually and verbose using inconsistent and incompatible implementations.
I fail to see why everyone here are so negative to actually standardizing something everyone is already doing anyway.
Someone please enlighten me?
We've been hearing for so many years that our bread and butter language was terrible, so we told ourselves that other people just hadn't spent the time to take advantage of the less commonly understood features of JS (not unique, just lest common among popular industry languages like python, java, etc). This leads to people claiming that prototypal inheritance always better than classical inheritance (everyone else who uses classes are just misguided), or that JavaScript is "functional" just because it has first class functions.
I used to feel this way about classes too. Now that I've started using them at work I feel like there is a great desire to justify the amount of time that has been spent hacking on prototypal inheritance to behave like classes.
Now that the newest versions of ES brings classes out of the box the point is moot. Use the one you are comfortable with or the one your team agreed with.
I'd think it should be:
// ES5
C.m3 = function(p3) { B4; }
// ES6
class C {
...
static m3(p3) { B4; }
}On the contrary though, since classes are carefully designed to be just syntactic sugar around prototypes, it should be easy to migrate, right?
Well no, because the one thing that became obvious was that, since there were no standards, and no clearly and obviously idiomatic way to do prototype chains, nobody was doing them in a correct, consistent way.
So trying auto-migrate is almost certainly doomed except in trivial cases, because as the article implies there's a high chance of it just being flat wrong anyway.
Ohhh boy. I predict trouble ahead.