3 ms·
You don't need all aspects of OOP ever to get use out of some of its concepts. ES2015 classes are much cleaner and easier for newbies to grok than messing with
by Andrex 9y ago
You don't need all aspects of OOP ever to get use out of some of its concepts. ES2015 classes are much cleaner and easier for newbies to grok than messing with the prototype chain, which was the way that sort of thing was achieved before.
Classes also make a lot of sense for custom elements (Web Components.) See Polymer v1 compared to v2.
- jorblumesea 9y agoEs2015 classes are just syntactic sugar around prototypically inherited objects. I agree they are nicer to use but under the hood there's no change. Which is why adding all of these oop concepts seems contrary to how the language is designed. At the end of the day JS has no true classical classes. Not an expert just my opinion.
- Andrex 9y agoGood point! I do think that private properties is a worthwhile endeavor though, even if it's more of a "hard OOP" concept that on the surface conflicts with the historical ES spec.
- erikpukinskis 9y agoES2015 classes are not easier to learn for newbies, they're easier to learn for Rubyists and other people coming from OO languages. There's nothing hard about this: function Person(name) { this.name = name } Person.prototype.greet = function() { return "Hi, "+this.name } var me = new Person("Erik") console.log(me.greet()) The syntax hurts your brain if you're used to looking at something else, but the control flow is actually very simple and totally transparent, unlike ES6, which does magical things that can't be understood by thinking about where the thread is moving.
- jhall1468 9y agoThere's zero magic. It is literally syntactic sugar and nothing else. Calling that magic is dishonest.
- erikpukinskis 9y agoOk, I can agree and I'll tighten up my use of "magic", reserve it for Railsy stuff where there's a lot of cpu cycles behind the scenes. It's an extra layer of indirection that you can't see in the debugger though. Not sure what to call that. I think calling me dishonest is a little strange. How is poor word choice dishonest?
- ubertaco 9y agoWhich is easier, explaining to your hypothetical total beginner what "Person.prototype" is and going down the rabbit hole of prototype-inheritance, and explaining how Javascript functions (unlike just about every other language's functions) can have attributes on them and can act like fake constructors and what "this" means in your example (and then later what it means in a different case, since it means so many different things)...or simply saying "yeah, we can define a new category, or 'class', of things called Person by saying 'class Person { ....'"?
- erikpukinskis 9y agoYou only think "class" is self explanatory because you already learned what it means.
- ubertaco 9y agoYes, but plenty of other non-programmers also learned what it means, when they first heard the English word. So it's not a huge conceptual leap. Whereas "prototype" has completely different English dictionary meaning, and thus requires in-depth redefinition.