mat byrne .com web design & development

Extending Functions in Javascript

Lately I’ve been writing a lot of Javascript, definitely a trend that I expect to continue for the foreseeable future. While working on a recent project I came across the following problem:

var condition = another_condition = true; // For this example to work!

function animate (callback)
{
    c = (typeof(callback) == 'function') ? callback : function () {};

    if (condition) {
        c = function () {
            c();
            console.log('bar');
        };
    }

    if (another_condition) {
        c = function () {
            // More functionality.
            console.log('baz');
            c();
        };
    }

    c();
}

animate(function () { console.log('foo'); });

Read more »