Skip to content

Javascript Lazy Function Definition

by Topper on April 29th, 2008

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:

[js]
var foo = function() {
var t = new Date();
foo = function() {
return t;
};
return foo();
};
[/js]

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