forked from nkronlage/JavaScripture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.jsdoc
More file actions
67 lines (55 loc) · 1.51 KB
/
proxy.jsdoc
File metadata and controls
67 lines (55 loc) · 1.51 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
Proxy : Object
The Proxy allows you to wrap an Object (called the **target**) and
modify the behavior of that Object.
Version:
ECMAScript 2015
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-proxy-objects
----
new Proxy(target : Object, handler : ProxyHandler) : Proxy
Creates a new Proxy for the **target** where **handler**
allows you to modify the behavior of **target**.
<example>
// Create a Proxy for Map that only allows even number keys
var evenMap = new Proxy(new Map(), {
// The 'get' function allows you to modify the value returned
// when accessing properties on the proxy
get: function(target, name) {
// Return a custom function for Map.set that checks
// that the keys are even numbers
if (name === 'set') {
return function(key, value) {
if (Number.isInteger(key) && (key % 2 === 0)) {
return target.set(key, value);
}
else {
throw Error('key "' + key + '" is not even');
}
};
}
else {
// Return the normal property value for everything else
return target[name];
}
}
});
evenMap.set(0, 'foo');
console.log(evenMap.get(0));
evenMap.set(2, 'bar');
console.log(evenMap.get(2));
try
{
evenMap.set(3, 'baz');
console.log(evenMap.get(3));
}
catch (e)
{
console.log(e);
}
</example>
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-proxy-target-handler
----
revocable(target : Object, handler : Proxyhandler) : Proxy
Spec:
http://www.ecma-international.org/ecma-262/6.0/#sec-proxy.revocable