JavaScript Doesn’t Need Class

Reprint from  I, Programmer

JavaScript is the single most important programming language and yet, as it reaches its high point, everyone is complaining about it and there are significant efforts to replace it with something better – Go, Dart, Coffeescript. Even the people who love it seem to misunderstand it because they want to add “class”. JavaScript doesn’t need class! And if you think it does you need to look more carefully at JavaScript.

I often have conversations with JavaScript programmers and make the point that the language needs to be treated as itself rather than as a poor relation to Java or some other language. They usually agree vigorously and then go into details about how some framework or other gives them facilities just like class and inheritance – just like Java or C# or… Well you get my point, or I hope you do.

JavaScript really is a very different language.

It is one of the few mainstream languages that doesn’t simply mimic the C/C++ way of doing things. It is also quite different in terms of its treatment of objects. The problem here is that programmers tend not to spend the time to find out what these differences are. They simply notice that it doesn’t have “class” and it doesn’t seem to have inheritance and then they spend lots of time inventing ways to add them or, worse they propose an upgrade to the language that adds them.

First off it isn’t clear that “class” is the best way to work with objects, and it certainly isn’t clear that class-based inheritance is even good, let alone the best.

We are simply following the conventions of what already know when identifying these omissions in JavaScript. Class-based programming was invented to make compilers work better and to make errors detectable at compile time rather than run time.

The argument goes that in a strongly typed, class-based, langauge you can’t assign apples to oranges because they are different types. This is fine, but the real question is why any programmer would make the mistake of assinging apples to oranges; and what is it about the way apples behave that make this even thinkable. Is it that perhaps apples have a method in common with oranges that suggest that they can be used in the same way? In which case perhaps the language should make an attempt to work out what the programmer intended rather than just flagging a type error.

More on JavaScript:

1 JavaScript Doesn’t Need Class

2 A JavaScript TimeInterval object

3 Speed dating – the art of the JavaScript Date object

4 Objects with Values in JavaScript

5 JavaScript data structures – the associative array

6 JavaScript Bit Manipulation

7 JavaScript data structures – a Lisp-like list

8 Treat JavaScript right!

9 Javascript data structures – the binary tree

10 Overriding a JavaScript global function – parseInt

11 Javascript data structures – Stacks

12 Why JavaScript is a Jem

13 Javascript data structures – a collection object

14 Javascript Jems – Prototype Inheritance

15 Javascript Jems – The Prototype

16 Javascript Jems – Type and the constructor

17 Javascript Jems – Object factories, constructors and clones

18 Javascript Jems – a new take on objects

19 KML in Google Maps and Earth

20 Javascript Jems – First class functions

21 Javascript Jems – Asynchronous Patterns

22 Javascript Jems – Lambda expressions

23 Getting started with Jetpack

24 Javascript Jems – self modifying code

25 Javascript Jems – active logic, truthy and falsey.

Advertisements

One Comment Add yours

  1. concierge says:

    hello,i would like to thank you for your discuss,it’s very useful,thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s