Why I want Classes in JavaScript

| 8 Comments

I had a little twitter rant this morning about classes in JavaScript. (Actually, the rant was really about the attitude that seems to be behind much of the "don't add classes to JavaScript" commentary I see.) So I should probably explain in somewhat longer form why I want classes added to the language.

  • The JS standard library is object oriented: we write a.map(f), not map(a,f). If the standard library defines classes, the language should make it easy to define classes.
  • A number of popular JS libraries include convenience methods for defining and/or extending classes. This demonstrates to me that developers want to use classes and JavaScript, but that they want a simpler way to define them.
  • Once we get used to the idea of a class keyword in the language, I think it is going to be really nice to be able to dash off little classes with that keyword, instead of the two step define the constructor and then set up its prototype object technique that we use now. In particular, I think it is going to feel really old-fashioned to define classes the old way once we've got modules in the language. I can't articulate this point well, but it is actually the one that is most compelling to me. We're getting modules in the next version of JS, and I don't think that the language will feel right without a class syntax to go along.
  • The classes being proposed for JavaScript are all syntax sugar for existing patterns. Adding a class keyword will not change JS in any fundamental way, it will just make it easier to use the patterns that we already use. If you like defining your classes the old way, you'll still be able to do that. And you'll still be able to add methods to classes with C.prototype.new_method = function() {...} as you can today. As Brendan Eich would say, this is just "paving the cow paths". Here's what the classes proposal [Update link fixed now] describes as its motivation:
    ECMAScript already has excellent features for defining abstractions for kinds of things. The trinity of constructor functions, prototypes, and instances are more than adequate for solving the problems that classes solve in other languages. The intent of this strawman is not to change those semantics. Instead, it's to provide a terse and declarative surface for those semantics so that programmer intent is expressed instead of the underlying imperative machinery.

    I understand the reluctance that many JS programmers feel about adding classes. I feel it too, a little bit. A class keyword isn't "javascripty". I like, and understand the appeal of Allen Wirfs-Brock's approach: He defines extensions that make object literals more expressive, and then uses the <| and {. operators to create a simple class definition pattern. But the problem is that it is still a pattern rather than a new syntactic form. And to quote Dave Herman: "patterns are the sincerest form of feature request": if we're using patterns to define classes, then the language ought to just support classes.

    My desire to have classes in the next version of JavaScript of course has no bearing on whether we'll actually get them. The classes proposal linked above is apparently not doing well in committee, for reasons that I haven't tried to understand. There have been recent attempts to revive classes with simpler class proposals, so maybe there is still some hope.

8 Comments

JSON is object data from the server.

Javascript is a client side language that for many of us is used to drive widgets, which are objects.

When all is said and done, javascript programing is about objects.

A language that emphasizes workarounds to achieving the very tools we need to build javascript apps rather than providing us with the actual tools is a language that needs a bit more work...

Re: your first point: Does object oriented always mean Class-based?

@JG

No, there are generally 4 flavours of OO, class and prototypical are two of them.

The syntax in JS for classes is more roundabout than it has to be. I doubt anybody would disagree. It's part of JS's power though once you understand how to use it correctly.

But I think that for people who want a simpler system for setting up classes, using languages like Coffeescript/Dart that compile down to JS are a better solution than changing up the fundamental nature of JS that will take browser makers another few generations to get perfect.

Given how long its taken Javascript itself to be adequately supported, I'm very hesitant about changing the language completely at this time.

Instead of saying "let's make JS more Java like with keywords class, public, private, extends ..." why not saying "let's make it more prototypal with specific keyword (if needed)".

Adding classes without changing the prototypal and typeless nature of JS will only add confusion for peoples coming from "classic" OOP.

But if you want to change the language (and it will change cause as end-user we have no power against google, Moz, evangelist & gurus), it will be better to introduce new keywords than the ugly <| (why not Santa Claus hat *<| )

@David

Aren't you afraid that adding class sugar will make confusion even bigger ?
We will have "class" for what's not really class in other popular languages.

I'm rather against adding "classes" to JS, but main reason is that I would like JS to expand towards functional programming, not OOP as we know it from Java, and if we will expand JS in two directions we will make confusion even bigger.

For years I've been programming in JS with class emulation methods, and it was a bit of struggle (like using incomplete language), last year I moved towards more functional thinking and Object.create, now I find that prototypal inheritance is really enough, one thing I'm missing is 'super', that's it.

The href attribute of your "classes proposal" link could be improved with a little content.

@Ed: thanks for letting me know about the broken link. Fixed now.

@Mariusz: I'm not worried about confusion. JS classes are different than in other languages, but JS programmers already know that. And they already use classes. I just think that simpler declarative syntax would be nice. Nothing will prevent you from doing functional programming in JS. But since JS supports classes, it ought to support them well.

@fpiat: No one is proposing making JS anything like Java. We happen to have Java keywords reserved, which is why proposals for extending JS often use keywords like 'class', 'public', 'extends', etc. But that is a matter of surface syntax. The class proposals that I am interested in simplify the creation of classes that use prototypal inheritance.

Books

ECMAScript 5 & HTML5!

"A must-have reference"
Brendan Eich,
creator of JavaScript

JavaScript graphics makes web programming fun again!

Read Less, Learn More

Comprehensive coverage of Ruby 1.8 and 1.9

"The New Most Important Ruby Book"
Peter Cooper,
rubyinside.com

The classic Java quick-reference

About

Advertising

Pages

Hosted By

Powered by Movable Type 4.21-en