-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06.js
More file actions
33 lines (29 loc) · 695 Bytes
/
06.js
File metadata and controls
33 lines (29 loc) · 695 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* 代理模式
* 缓存代理
*/
function test1() {
// 原函数
var multi = function () {
var a = 1;
for (var i = 0, l = arguments.length; i < l; i++) {
a = a * arguments[i];
}
console.log(a);
return a;
}
// 代理函数
var proxyMulti = (function () {
var cache = {};
return function () {
var args = Array.prototype.join.call(arguments, ',');
if (args in cache) {
return cache[args];
}
return cache[args] = multi.apply(this, arguments);
}
})();
proxyMulti(1, 2, 3, 4, 5);
proxyMulti(1, 2, 3, 4, 5);
}
test1();