The b(ack)log

Brushing up on JavaScript

I working on an application that is mostly client side and I need to brush up on my JavaScript skills a bit. Here are some notes on the experience.

One of the best resources I found was A re-introduction to JavaScript from the Mozilla Developer Network.

The next thing I would like to point out is promises, definately one of the more usefull things you’ll learn about JavaScript. I’m using the JQuery Deferred object which according to my understanding implements the promise interface.

Working in async world is not completely new to me, I’ve developed some stuff in HaXe targetting the Flash runtime and I’ve developed a kiosk application using QML for the interface. Promises are a big step forward:

    return getData();
    return getSomeOtherData(res);

Some useful JavaScript functions that I met along the way was apply and bind.

Amongst things apply is good for calling a function with list of arguments that you are computing. For instance, maybe you want to get a list of files to copy, then copy all the files and after all that is finished you want to do something else.

var args = [];
// build your array of args
// args.push( function (res){ ... blah ... } );
$.when.apply(null, args);

And I used bind to make a functor that I can pass to be called.

doSomethingWithCallback(copyBlahBlah.bind(null,arg1, arg2));

There’s something more magic hiding with bind - you don’t need to pass all the arugments. They call it currying, I think no amount of code can compare with a proper Durban curry.

var f = function(a, b, c) {};
var f2 = f.bind(null, [1, 2]);

Obviously there are lots more that I need to catch up on and learn!

comments powered by Disqus