201707271123_《Javascript的‘缓存’》

function memoizeFunction(func) {
    var cache = {};
    return function() {
        var key = arguments[0];
        if (cache[key]) {
            return cache[key];
        } else {
            var val = func.apply(this, arguments);
            cache[key] = val;
            return val;
        }
    };
};

function add(x,y){
   var result = 0;
	this.x = x;
    this.y = y;
    result = this.x + this.y
    console.log(result);
};

memoizeFunction(add(19,3)); //22

memoizeFunction(add(19,3)); //还是22, 但从缓存中读取,速度快

  

posted @ 2017-07-27 11:25  Coca-code  阅读(150)  评论(0编辑  收藏  举报