My Book

执行一次函数,再次调用,访问同数据

var fn = (function(){
        var cache;
        var ran = Math.random();
        function init(){
            return ran;
        }
        if(!cache){
            cache = init();
        }
        console.log(cache);
        return cache;
    })();
    console.log(fn);
    console.log(fn);
    console.log(fn);

利用缓存和自调用函数