News for hackers. Stuff that matters.
Re:ES6 in Depth: Classes by Cshelton on Thursday July 23, 2015 @01:49PM (#9937336)
Hopefully this will clear the whole "class" thing in javascript up with some people. Every time I mention using classes in Javascript I get the obligatory response, "That's stupid, they are not actual classes, just syntactic sugar. I'm not using that!" face palm

Yes, I know...but look at how much nicer that is to type than his first example...

Pretty good, straight forward write up though!

Re:ES6 in Depth: Classes by sago on Thursday July 23, 2015 @03:16PM (#9937854)
It's a Faustian bargain. It blesses Javascript with a wider audience, less initial balking by new JS programmers, and a few characters for established JS programmers. But it does so by deliberately misleading programmers by naming the syntax to be the same as the thing they think they want, but JS doesn't contain. That seems like a dick move that condemns a whole slew of new developers to permanent misunderstanding of what they're writing, and a fundamental lack of clarity on prototypical inheritance.

I will use it. Because syntactic sugar in this case does save some work. But I'm very conscious the code has swapped superficial clarity for deep lies.

I suspect Stack Overflow will overflow with even more 'javascript OO is broken' questions and the MDN article won't make more than a dent on clearing up that (deliberate) confusion.

I agree with Crockford on this.

Re:ES6 in Depth: Classes by coldtea on Thursday July 23, 2015 @07:49PM (#9939220)
There's no reason to confine "class" to just mean C++/Java style inheritance, just because it was used that way first.

Besides, most people used prototype based inheritance in this exact manner, so it's good to codify and give a concrete implementation, instead of 200 ad-hoc remakes of the same thing.

Third, people still get all the prototypal flexibility anyway if they want it.

Re:ES6 in Depth: Classes by sago on Thursday July 23, 2015 @08:15PM (#9939321)
> instead of 200 ad-hoc remakes of the same thing

I think that's a good argument for including the syntax, though it raises me from 'anti' to 'ambivalent'. But yeah, figuring out random coder's favourite OOP implementation style is an annoyance. Here Crockford seems to be a bit part of the problem: he seems to recommend a whole new approach every couple of years. His recommendations to never use 'new' (and now, don't use 'this') make it a pain to use other people's objects.

I wonder if a better consensus might be the best of all worlds.

Re:ES6 in Depth: Classes by wnevets on Thursday July 23, 2015 @11:26PM (#9939988)
> he seems to recommend a whole new approach every couple of years

you make that sound like a negative. If you find a 'better' way, why not use it?

Re:ES6 in Depth: Classes by sago on Friday July 24, 2015 @12:43AM (#9940150)
Indeed, the problem is on code bases or teams with multiple ways of doing the same thing. I define new objects with.

    var foo = Object.create(bar);
You do

    var foo = new Bar();
Amy does

    var foo = Bar();
and Ellen does

    var foo = Bar.create();
For many values of 'better', the advantage is much smaller than the additional burden.

Thus I have no problem with him finding better ways, but his influence and his way of presenting things as the one best solution causes issues, in my experience.

Re:ES6 in Depth: Classes by abecedarius on Thursday July 23, 2015 @09:11PM (#9939526)
Well, I was using thisless style all along, and it's nice to get company. I'm a little saddened 'class' will probably win out.
Re:ES6 in Depth: Classes by chimeracoder on Thursday July 23, 2015 @09:07PM (#9939509)
> That seems like a dick move that condemns a whole slew of new developers to permanent misunderstanding of what they're writing, and a fundamental lack of clarity on prototypical inheritance.

Do you feel the same way about Python's use of the word 'class'? Because Python also uses prototypical inheritance under-the-hood. The abstraction is clean enough that most people don't know the difference, but it still really is "just" prototypes down there if you peel back the layers.

Re:ES6 in Depth: Classes by sago on Friday July 24, 2015 @01:02PM (#9943418)
Python didn't used to have such flexible classes. It has migrated that way over time. So 'dick move' doesn't seem to be a valid criticism, to me.

That said, Python is a hybrid under the hood. It has become more prototypal over time, but still enforces the distinctions between classes and instances.

I personally think that the opportunity to fundamentally open Python to a better object model was missed when Metaclasses were added. I like Metaclasses in Python, I think they need to be there, and help all kinds of tasks. But it might have been better still to support full prototypal inheritance at that point.

The complaint I'm making is definitely true of Python though, because it dresses its objects in the outfit of classes and instances, few programmers end up understanding the underlying model and how it can be used to improve their code. And, because of that, it is unwise to use those techniques in code that will be written and maintained by a team. I tend to get nervous of code that uses custom Metaclasses, for example, because of the burden of education is imposes on a project.

I'd prefer a situation where Prototypal inheritance was actually taught as the Javascript approach. But perhaps that is utopian and naive.

Re:ES6 in Depth: Classes by TazeTSchnitzel on Thursday July 23, 2015 @03:33PM (#9937941)
The flexibility of prototypical inheritance is one of JavaScript's greatest assets. JavaScript: The Good Parts includes several different approaches to OOP which prototypes make possible.

`class` just picks one and pretends the rest never existed.

Re:ES6 in Depth: Classes by chc on Thursday July 23, 2015 @03:58PM (#9938110)
I don't see what's wrong with that. Converging on one way of doing things is part of the purpose of a standard. We still have the option of implementing a different method if we prefer, just as much as we ever did.
Re:ES6 in Depth: Classes by angersock on Thursday July 23, 2015 @05:18PM (#9938596)
If it's truly a standard, there should be no other options.

Instead, this sets the stage for lots of OOPish JS which feels very different from a lot of existing code out there, and will result in awkward shims to interface the different paradigms.

We already see such ugliness in C++.

Re:ES6 in Depth: Classes by chc on Thursday July 23, 2015 @05:39PM (#9938693)
> If it's truly a standard, there should be no other options.

That's a bit strong. Most languages come with a standard set of data structures, but that doesn't mean they make it impossible to implement other data structures if it suits you (e.g. few languages include binary trees as a standard data type, but that doesn't mean you can't use them).

C includes standard string algorithms, but that doesn't mean you can't write your own — you can even create your own string type if C strings are problematic for some reason.

Many things in many languages are there to offer a reasonable default that is generally useful. This seems along those lines to me.

Re:ES6 in Depth: Classes by coldtea on Thursday July 23, 2015 @07:51PM (#9939226)
>If it's truly a standard, there should be no other options.

That has never been the case with most standards. No see why start now. If it's a standard, it should just be present to all engines, and (but not necessarily) the most popular option.

Nothing about it being a standard necessitates it should be "the only option".

>Instead, this sets the stage for lots of OOPish JS which feels very different from a lot of existing code out there

Quite the opposite. This sets the stage for finally having code that looks THE FUCKING SAME as other code.

Instead of 200 ad-hoc implementations of the same exact OOP pattern -- which is what we have now.

Re:ES6 in Depth: Classes by frik on Thursday July 23, 2015 @03:36PM (#9937964)
Exactly! Sadly many misunderstand JavaScript and don't even know or misunderstand prototype-based inheritance: https://en.wikipedia.org/wiki/Prototype-based_programming

Lua is another well know language that has prototype-based inheritance.

Re:ES6 in Depth: Classes by frik on Thursday July 23, 2015 @03:28PM (#9937919)
I agree with Crockford too. It's Microsoft, Eich & co that pushed the class syntax based on ideas that originated in TypeScript.

I am very grateful to Crockford for the "Good Parts" book and JS5 - I like that lean approach.

Re:ES6 in Depth: Classes by spion on Thursday July 23, 2015 @06:32PM (#9938911)
Seems like people keep saying this again and again, despite the fact that there is no single universal implementation of "classes".

Are Python classes broken as well?

Re:ES6 in Depth: Classes by gwt on Thursday July 23, 2015 @06:35PM (#9938924)
I couldn't help but laugh when I read your comment and noticed that the author's last name is Faust.
Re:ES6 in Depth: Classes by jonahx on Thursday July 23, 2015 @02:54PM (#9937745)
Douglas Crockford says that classes were the single worst mistake in es6. So it's not really a face palm:

https://www.youtube.com/watch?v=PSGEjv3Tqo0&t=5m

Re:ES6 in Depth: Classes by jtuchsen on Thursday July 23, 2015 @03:42PM (#9937997)
Is his entire complaint that the syntax looks too much like Java? The fact is, people are already using prototypes to share common methods across objects, and classes are really nice short syntactic sugar for doing that. His comment on classes there is too flippant to even know the meaning of his complaint.

Beyond that I'd argue that classes give you more guarantees about how an object will behave at the expense of flexibility, although they give you escape hatches if you like. The reduced flexibility is really nice because you can define safe subsets of the language in which tools that analyze and refactor code are possible, even across files if you're using the module system. Moving the language in a direction where consistent universal tooling is possible to write is something that I love about classes, and is often overlooked in discussions about it. I don't care that classes are just a simplified subset feature of what's possible using prototypes, it covers the common case well enough and makes it possible to write really nice code completetion and refactoring tools that don't randomly break. That's worth all the extra magic in my opinion.

Re:ES6 in Depth: Classes by roebk on Thursday July 23, 2015 @04:57PM (#9938470)
Personally I find the OLOO style much simpler, cleaner system. I'd be interested to know why you'd choose class over Object.create()
Re:ES6 in Depth: Classes by jtuchsen on Thursday July 23, 2015 @06:19PM (#9938851)
People do weird metaprogramming things with the existing constructs. In a lot of cases it's because people prefer to keep things DRY, but it hurts when you're trying to grep through a large codebase to find that one method you need. A contrived example:

function Animal() {}

function Cat() {}

Cat.prototype = Object.create(Animal.prototype);

(function(fields) { fields.forEach(function(field) { Cat.prototype[field] = function() { // Some nonsense }; }); })(['what', 'the', 'hell']);

It's impossible to statically infer what methods are being declared in ES5 code in general. You can pick up on some common assignment patterns, but not all the weird stuff people do. Just check out the top 10 NPM modules and I guarantee you'll find some odd assignment patterns. I can remember that the "colors" module did meta stuff when I last checked. You can't just easily run the code to find out what the module is exporting either, because of potential side effects from IO. You can wrap Node's core IO modules, but that solution is specific to Node, and still won't cover every possible case. In short, it's a huge pain to write tools to pull information out of Javascript code in general, and that hurts the tooling ecosystem around Javascript.

With the class pattern you at least have some staticly inferable information you're sure you can pull out of the class, and weird stuff is discouraged. I know in some ways that this is a weak argument, because of the new square bracket assignment, and the fact that anyone can muck with the prototype of the class after it's created. Even still, for the simple OLOO case it does encourage people to write code that has method names that can easily be staticly inferred, and that's a good thing for JS tooling possibilities.

I really just want omnicompletion that doesn't randomly break on me! The OLOO pattern is one that's incredibly common in JS

Re:ES6 in Depth: Classes by wnevets on Thursday July 23, 2015 @04:47PM (#9938410)
I gotta agree with him, the class sugar is just the continuation of the 'new' mistake.
Re:ES6 in Depth: Classes by mhd on Thursday July 23, 2015 @03:07PM (#9937812)
There's no rest for the Java programmer, now even JS coders join the SmallTalk/Lisp elite…

Is Crockford's dislike still based on this: http://www.crockford.com/javascript/inheritance.html ?

Because I don't quite buy into that line of thinking. I'm really looking forward to webassembly…

Re:ES6 in Depth: Classes by voyou on Thursday July 23, 2015 @04:11PM (#9938184)
I think Crockford is wrong in that linked article. What he presents as unique features of prototype-based OO seem to really be features of highly dynamic OO. All of the things he implements in that article could be implemented just as easily (indeed, in more-or-less the same way) in Python or Smalltalk, couldn't they?
Re:ES6 in Depth: Classes by noobermin on Thursday July 23, 2015 @09:49PM (#9939679)
That was actually a great talk, I'm happy I'm not the only one in the world that doesn't use new...and the dec64 idea sounds freaking awesome
Re:ES6 in Depth: Classes by fallat on Friday July 24, 2015 @12:41AM (#9940145)
It is important they understand what is really happening though, or else they're in for a few surprises.