Last active
December 4, 2015 07:36
-
-
Save Janking/c81ec0de40fd00b94939 to your computer and use it in GitHub Desktop.
缓存代理模式
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mult = function() { | |
console.log('开始计算乘积:'); | |
var a = 1; | |
for (var i = 0, l = arguments.length; i < l; i++) { | |
a = a * arguments[i]; | |
} | |
return a; | |
} | |
var sum = function(a,b){ | |
return a+b; | |
} | |
var createProxyFactory = function(fn) { | |
var cache = {}; | |
return function() { | |
var args = Array.prototype.join.call(arguments, ','); | |
if (args in cache) { | |
return cache[args]; | |
} | |
return cache[args] = fn.apply(this, arguments); | |
} | |
}; | |
var proxyMult = createProxyFactory(mult); | |
var proxySum = createProxyFactory(sum); | |
console.log(proxyMult(1, 2, 3, 4)) //24 | |
console.log(proxySum(2,3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment