返回列表

惰性函数Lazy Function Definition Pattern

默认分类 2011-11-22 23:40:14

转载: http://michaux.ca/articles/lazy-function-definition-pattern

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

var a = {};
a.foo = function() {
   var t = 5;
   a.foo = function() {
      return t++;
   };
   return a.foo();
};
alert(a.foo());
alert(a.foo());