Wednesday, January 13, 2010

The bumps on the road to javascript

 

I'll say it.  

The Rhino Book is a bad way to learn Javascript.  It often gets recommended as the de facto book on javascript, but it's too much heft that doesn't point out what really separates Javascript from other languages.

 

When I first heard about javascript, it was back in the 90's, when most examples for it was to put snowflakes on the screen and trails of kittens that followed your cursor.  In the last 4 years; AJAX, jQuery, V8 javascript engine, node.js, server-side js with Rhino all contributed to javascript's growth.  I'm picking javascript for client-side implementation because of its ubiquity, flash doesn't have a future(1), and its ability to teach me something about programming.

I haven't brought some of the newer js books like the good parts, or ninja, but I was able to spend time going over John Resig's Advanced Javascript tutorial.  Resig is as much of a force for good for Javascript as _why was for Ruby, by being able to teach others.  I'd recommend going over that tutorial, as it cleared some bumps along the way I had about understanding basic parts of the language.  

Here's some of them.  Maybe it'll help someone else out.  I'll keep 'em short.

Scope

Javascript scope had always tripped me up.  In languages like C, Java, and Ruby, scope was usually pretty apparent, because you can see it in the code, sectioned off by block delimiters.  Variables can see out, but they can't see in.  In javascript, it's the same, except this (or in other languages, self), changes when you call a function.  This was especially frustrating in cases where I was using map functions in javascript.  This would change inside of the anonymous block passed to map.   

 

 

There are already a number of scope tutorials on the web. I won't repeat what they say, but what helped was understanding that This changes depending on who calls the function. Scope and context of this can be controlled on a function by function basis

Prototype-based Object Orientation

Reading the Rhino book's Prototype based inheritance was confusing.  Instead, it was easier to think of javascript's object model was more like how CSS worked.  You have an object that served as a template (or aptly named, prototype), and if you need to override any properties, you do so.  Anything missing is looked up in the prototype.  I don't know why this was that hard.  It must have just been the name.  I thought it was something completely different.

Javascript has very few core elements

That you can use to build what you need.  At the core, there are functions, arrays, and objects.  All three of them have properties, thought of as 'slots' you can put variables, functions, arrays, and objects in.  What makes it more like lisp is that you can treat functions as data you can pass around, as first class citizens.  Javascript's core minimalism really reminds me of Lua.  I much prefer Lua, and would have preferred it to be the lingua franca of the web, but Javascript is good enough for now.

That said...

Naked javascript really sucks.  Some mix of jQuery, Underscore, Functional, Prototype, MochiKit, or Closure should really be parts of the core language.  Without each, map, and reduce, I find having to write for loops to be a pain.  If people from multiple libraries are implementing the same basic things, that's a good indication it's a weakness of the language.  I'll use for loops when I need to optimize something later.

Javascript is still missing some features from other languages, like tail-call optimization.  It's like it doesn't want to admit it's a functional language (albeit unpure).  It also has no method_missing call.  Firefox has implemented a __noSuchMethod__ call, but it's non-standard.  I haven't found a way to replicate it with its current features yet either.

I'm sure there are others, but I haven't gone deep enough into the rabbit hole yet.  I still find some things about javascript pretty grating, but the platform it sits on--the browser and the web--makes it pretty exciting.  

(1) When I say doesn't have a future, it'll still be around, but like the way radio is still around as a medium when TV and internet came around.

 

Posted via web from The Web and all that Jazz

1 comment:

  1. Christopher Lee6:46 PM

    i've heard really good things about javascript: the good parts. my friends at thinklink, who i'd consider javascript wizards, recommend it.

    ReplyDelete