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

The JavaScript RegEx API

Regular Expressions are about as elegant as a pig on a bicycle. Using a regular expression feels like resorting to machine code when all those patterns we’re taught to love just aren’t up to the job. Which, I suppose, is also a reason to like them. They have a brute force directness, free from pattern politics and endless analysis.

And they work. Eventually.

If the JavaScript Regular Expressions API makes your head spin then this might be for you. I’ll document the basics and demonstrate how you might use them to full effect.

For the sake of brevity (not to mention my own lack of regex proficiency) I won’t discuss the syntax of the expressions themselves. Suffice to say, JavaScript regEx syntax is Perl based. There are many excellent online resources for this, as well as some nice online RegEx testers.
Continue reading

JavaScript Partials

In a previous post I introduced the curry function. To recap, currying creates a new function with the first n arguments pre-assigned:-

var subtract = function(a,b) {
    return a - b;
}

var subtractFrom8 = subtract.curry(8);

subtractFrom8(2); //6

Currying is an expressive and compact alternative to manually wrapping anonymous functions. I use it a lot. But sometimes its not enough – the problem is you can only pre-assign the first n arguments. What if we wanted to make a function and pre-assign the rightmost argument, or maybe the middle two? Enter partial:-

var subtract5 = subtract.partial(___,5);

subtract5(13); //8;

Continue reading

express yourself: rapid function definition

The native JavaScript function indexOf, applied to a String, returns the index of the first occurrence of a specified value. Useful as it is, I often find myself wishing for a variant method that will return the index after the occurrence so that I can pass the result to a slice call and return everything after a given character.

With this in mind, I created a method called express (as in expression, and alsoquick) which returns a new function by applying the expression argument to the result of the old function. Its partly inspired by the Haskell language and by Oliver Steele’s lambda function.

String.prototype.indexAfter = String.prototype.indexOf.express('r + 1');

Continue reading

JavaScript and valueOf

In JavaScript, valueOf and toString are sister methods inherited by every object. One of these methods will get invoked whenever an expression encounters a complex object where a primitive value was expected. For example :-

alert(myHamster);
var result = 2 + myHamster;

In broad terms, if the expression hints at the need for a string then toString is invoked,  otherwise its valueOf . If either method returns a non-primitive, the other method gets a try. The above examples expected myHamster to be a string and a number respectively so they are evaluated as:-

alert(myHamster.toString()); //interpreter expected a string
var result = 2 + myHamster.valueOf(); //expected a number

Continue reading