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:

JavaScript:
  1. var foo = function() {
  2.     var t = new Date();
  3.     foo = function() {
  4.         return t;
  5.     };
  6.     return foo();
  7. };

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

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS