Yesterday I released a small JavaScript module called laze for defining lazy properties in JavaScript. With laze, you can have properties of an object not be created until they are accessed.

var Thing = function () { };

Thing.prototype = {
  prop: function () {
    // some expensive operation
laze.make(Thing.prototype, 'prop');

Once you have it set up, you can access “prop” as a property instead of as a function:

var thing = new Thing();
thing.prop; // function is run now
thing.prop; // and cached forever