[1+1=2]

OneAndOneIs2

« Better still2011: An open letter »

Wed, Dec 28, 2011

[Icon][Icon]The short book

• Post categories: Omni, FOSS, Rant, Technology, My Life, Programming

I hate Javascript.

Mostly because of things that aren't really its fault, to be fair:

  • The DOM is ugly, so JS DOM manipulation is inherently ugly.
  • Most of the JS you encounter is badly-written, because too many people just cobble it together until it seems to do what they want, instead of learning how to write it well before they start.
  • It's so disorganized and messy. A function gets tripped when you edit a form field. Why? Maybe it has an onclick on that element. Maybe there's an onchange on the form. Maybe a function that was run when the page loaded bound that element to that function. Or maybe it was bound by a function called by a function called by a function... it's just a mess.

But when you look under the hood.. it's no better! Global variables, weird scoping (A helper function defined inside a method gets the global object bound to 'this' instead of the object bound to the method's 'this', FFS), godawful class syntax...

So when somebody at work recommended a book called "JavaScript: The Good Parts" my immediate reaction was "It must be a very short book! Ha! Ha!"

It is.

150 pages, and 50 of it's appendices. Only 100 pages could be written about what's good about JS.

The blurb on the back of the book? Well, it begins:

Most programming languages contain good and bad parts, but JavaScript has more than its share of the bad

This seemed promising.

And it is. It's a damn good book. It throws out all the cruft and the useless "Hello, world" nonsense that most JS books blither on about, and just tells you about what JS got right and how to use it properly.

Don't use the class syntax. JS isn't classical: It's prototypal. An object isn't an instance of a class; you make a new object by linking to the prototype of the type of object you want.

Don't think of it as anything to do with Java, don't think of it with your C hat on. It's more functional than procedural: Put your Lisp/Haskell/Higher Order Perl hat on. Don't think about global variables, think about data stored in closures.

And suddenly if you want, say, iterator objects, you don't have a bunch of global variables or bad class syntax. You can dump all of that and create objects with closures that you can pass around by reference. And it's almost like you're using a decent language again!

var Counter = function (start){
    this.counter = typeof start == 'number' ? start : 0;
    };

Counter.prototype.count = function (){
    return this.counter++;
    };

var count_zero = new Counter();
var count_hundred = new Counter(100);
var count_badly = new Counter('wibble');

As simple as that, you can now get the output:

- count_zero.count();
0
- count_zero.count();
1
- count_hundred.count();
100
- count_hundred.count();
101
- count_badly.count();
0
- count_badly.count();
1

All nice and sane. And, compared to what you usually see in JS, quite elegant.

If the rest of the book is as helpful as the first third, I live in hope that I may yet be able to add "Just use raw JS" to the existing list of "Use Jquery" and "Learn EXT" that I currently have available for making web pages dynamic.

No feedback yet

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
This is a captcha-picture. It is used to prevent mass-access by robots.
Please enter the characters from the image above. (case insensitive)
 

[Links][icon] My links

[Icon][Icon] Hmm.. new look for twitter? I hope it gets less "Ick! Change! Put it back!" nonsense than Facebook..
08/02/12

[Icon][Icon] Facebook Syndication Error
22/02/12

[Icon][Icon] I last listened to:
Johann Pachelbel - Canon in D major

[Icon][Icon] Most recent photo:
Submersible houseboat

[Icon][Icon]About Me

[Icon][Icon]About this blog

[Icon][Icon]My LQ profile

[Icon][Icon]My /. profile

[Icon][Icon]My Wishlist

[Icon]MyCommerce

[FSF Associate Member]


February 2012
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29        

Search

User tools

XML Feeds

eXTReMe Tracker

Valid XHTML 1.0 Transitional

Valid CSS!

[Valid RSS feed]

powered by b2evolution