Understanding JavaScript Arrays

(Russian Version is here)

What is an Array in JavaScript?

A numerically indexed map of values.

Traditionally an array reserves a continuous allocation of memory of predefined length. In JavaScript this is not the case. A JavaScript array is simply a glorified object with a unique constructor and literal syntax and an additional set of properties and methods inherited from Array.prototype. If this means we make a small sacrifice in performance, it is more than compensated for by its ease of use and the power of its utilities. Unlike its counterparts in certain other languages, JavaScript arrays are a joy to use – this is something they definitely got right.

Continue reading

Function Declarations vs. Function Expressions

Lets start with a short quiz. What is alerted in each case?:

Question 1:

function foo(){
    function bar() {
        return 3;
    }
    return bar();
    function bar() {
        return 8;
    }
}
alert(foo());

Question 2:

function foo(){
    var bar = function() {
        return 3;
    };
    return bar();
    var bar = function() {
        return 8;
    };
}
alert(foo());

Question 3:

alert(foo());
function foo(){
    var bar = function() {
        return 3;
    };
    return bar();
    var bar = function() {
        return 8;
    };
}

Question 4:

function foo(){
    return bar();
    var bar = function() {
        return 3;
    };
    var bar = function() {
        return 8;
    };
}
alert(foo());

If you didn’t answer 8, 3, 3 and [Type Error: bar is not a function] respectively, read on… (actually read on anyway ;-) )

Continue reading

Understanding JavaScript Timers

You’re probably familiar with Javascript’s built-in timer functions: setTimeout and setInterval. At face value their behaviour appears straightforward and predictable. However JavaScript’s single threaded nature can cloak these seemingly innocent little features with mystery and intrigue, not to mention hidden powers which can be harnessed for the greater good…

Continue reading

dipping into wu.js: autoCurry

It’s my pleasure to welcome our first guest blogger: Nick Fitzgerald is the author of the excellent wu.js “a lazy functional programming library”. It’s an inspiring resource with a lot of really original touches and very nicely written. Take it away Nick….

One of my favorite functions in my newly released wu.js library is wu.autoCurry, a function that takes a function and returns (you guessed it) a curry-able version of that function.

I am going to assume you are familiar with currying from here on out. If you aren’t yet, check out the Wikipedia article on currying for a general overview, and Angus’ piece on currying in Javascript for a detailed explanation in our current language.

Some languages, notably Haskell, automatically provide currying to the user. If you don’t pass all the arguments to a function, it is implied that a new function that will take the rest is returned. In Javascript, we don’t have that luxury. We are forced to write explicit, boilerplate code every time we want to curry a function.

Continue reading

Understanding JavaScript Prototypes.

(en Español)

JavaScript’s prototype object generates confusion wherever it goes. Seasoned JavaScript professionals, even authors frequently exhibit a limited understanding of the concept. I believe a lot of the trouble stems from our earliest encounters with prototypes, which almost always relate to new, constructor and the very misleading prototype property attached to functions. In fact prototype is a remarkably simple concept. To understand it better, we just need to forget what we ‘learned’ about constructor prototypes and start again from first principles.
Continue reading