-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.js
38 lines (34 loc) · 970 Bytes
/
proxy.js
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
34
35
36
37
38
/**
* Object.prototype.bound proxy: Cached function to object binding.
* Copyright (c) 2015 Vladislav Zarakovsky
* MIT license https://github.com/vlazar/object-bound/blob/master/LICENSE
*
* EXPERIMENTAL (!)
* Uses ES6 Proxy, which is currently only available in Firefox and Microsoft Edge
* https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Proxy
*/
(function () {
var bound = 'bound'
var property = Object.defineProperty
property(Object.prototype, bound, {
configurable: true,
get: function () {
var cache = {}
var proxy = new Proxy(this, {
get: function (self, name) {
if (bound === name) {
cache = {}
return proxy
} else {
return cache[name] || (cache[name] = self[name].bind(self))
}
}
})
property(this, bound, {
configurable: true,
value: proxy
})
return proxy
}
})
})()