Dom Parise

The art of Javascript


Asynchronous control flow in Javascript

Please note that my target audience are students who are learning web development, with little javascript experience, but are familiar with programming.

Functions in Javascript

Functions are first class objects.

function bar (a, b) { return a+b; };

var doSomething = bar;

doSomething( 1, 2);  // hint: 3

This affords the ability to pass functions as arguments to other functions, then either pass them further along, or execute them.

function foo ( fn ) {
    return fn (2, 3);
}
foo( doSomething); // hint: 5

They can also be anonymously declared. In javascript we call them anonymous functions; in objective-c they’re blocks; in python and c++ they’re lambda’s.

function ( c, d) {
    return c*d;
}; // hint: SyntaxError: function ( c,d) {
                                 ^

var bar = function ( x, y ) {
    return x-y;
}; 
bar( 5, 10); // now we're good

We also can use (...

Continue reading →