Skip to content

Commit f07d852

Browse files
committed
✨ Proxy Cache
1 parent b23d987 commit f07d852

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/structural/proxy/Sample-2.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// proxy cache【缓存代理】
2+
3+
const multiply = function(...args) {
4+
console.log("You can' see me if args in cache")
5+
return args.reduce((product, n) => product * n, 1)
6+
}
7+
8+
const proxyMultiply = (function() {
9+
const cache = {}
10+
return function() {
11+
const argsKey = Array.prototype.join.call(arguments, '@')
12+
if (argsKey in cache) return cache[argsKey]
13+
return (cache[argsKey] = multiply(...arguments))
14+
}
15+
})()
16+
17+
console.log(proxyMultiply(3, 4))
18+
console.log(proxyMultiply(3, 4))
19+
console.log(proxyMultiply(5, 4))

0 commit comments

Comments
 (0)