Javascript Lazy Function Definition

This is an oldy-but-goody.

If you have an expensive function in javascript that will always return the same results... make it do its dirty-work only once:

JavaScript:
var foo = function() {
    var t = new Date();
    foo = function() {
        return t;
    };
    return foo();
};

From: http://peter.michaux.ca/article/3556

No Comments Yet

You can be the first to comment!

Speak Your Peace

  • Comment Policy:Could go here if there's a nagging need Login Instructions: Would go here if there's a desire.