var once = (function(){ var res = {}, i = 0 ; return function(func){ if(typeof(func) != ‘function‘){ alert(‘参数类型不正确‘); return false; } for(var j in res){ if( res[j].fn === func ){ console.dir(‘read from cache...‘); return res[j].ret; } } return res[i++] = {ret:func(),fn:func}; } })(); var a1 = function(){ return 1;} var a2 = function(){ return 2;} once(function(){ return 1; });
时间: 2024-10-25 11:14:52