forked from nkronlage/JavaScripture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxyhandler.jsdoc
More file actions
277 lines (202 loc) · 6.2 KB
/
proxyhandler.jsdoc
File metadata and controls
277 lines (202 loc) · 6.2 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
ProxyHandler : Object
The ProxyHandler is used by %%/Proxy|Proxy%% to intercept and modify
the behavior of the proxied object. You may use any JavaScript object
as a ProxyHandler. If any of the following methods are not provided,
the Proxy will use the default behavior for that method.
Version:
ECMAScript 2015
----
instance.apply(target : Function, thisArg : Object, parameters : Array) : Object
Called when
**proxy(...)**,
%%/Function#apply|**proxy.apply(thisArg, parameters)**%%,
or
%%/Function#call|**proxy.call(thisArg, ...parameters)**%%
are called.
<example>
var x = function(arg1, arg2) {
console.log('in x(' + arg1 + ', ' + arg2 + ')');
};
var proxy = new Proxy(x, {
apply: function(target, thisArg, parameters) {
console.log('in apply');
return target.apply(thisArg, parameters);
}
});
proxy('direct1', 'direct2');
proxy.apply(null, ['apply1', 'apply2']);
proxy.call(null, 'call1', 'call2');
</example>
----
instance.construct(target : Function, parameters : Array) : Object
Called when **new target(...parameters)** is called.
<example>
var Button = function(text) {
this.text = text;
};
var ButtonProxy = new Proxy(Button, {
construct: function(target, parameters) {
console.log('in construct');
var button = Object.create(target.prototype);
target.apply(button, parameters);
return button;
}
});
var button = new ButtonProxy('OK');
console.log(button.text);
</example>
----
instance.defineProperty(target : Object, name : String, propertyDescriptor : PropertyDescriptor) : undefined
Called when %%/Object#defineProperty|**Object.defineProperty()**%%
is called on the proxy.
<example>
var x = { foo: 1 };
var proxy = new Proxy(x, {
defineProperty: function(target, name, propertyDescriptor) {
console.log('in defineProperty');
return Object.defineProperty(target, name, propertyDescriptor);
}
});
Object.defineProperty(proxy, 'bar', { value: 2,
writable: true,
enumerable: false,
configurable: true } );
</example>
----
instance.deleteProperty(target : Object, name : String) : Boolean
Called when **delete proxy[name]** is called. Return **true**
if the delete was successful.
<example>
var x = { foo: 1 };
var proxy = new Proxy(x, {
deleteProperty: function(target, name) {
console.log('in deleteProperty');
return delete target[name];
}
});
delete proxy.foo;
console.log(proxy.foo);
console.log(x.foo);
</example>
----
instance.enumerate(target : Object) : Array<String>
----
prototype.get(target : Object, name : String, proxy : Proxy) : Object
Called when reading the **name** property of the **target** of the proxy.
The return value is used as the value of the property.
<example>
var x = { foo: 1 };
var proxy = new Proxy(x, {
get: function(target, name, proxy) {
console.log('in get');
var value = target[name];
if (typeof value === 'string') {
value = value.toUpperCase();
}
return value;
}
});
console.log(proxy.foo);
console.log(x.foo); // value stored on x is unmodified
</example>
----
instance.getOwnPropertyDescriptor(target : Object, name : String) : PropertyDescriptor
Called when %%/Object#getOwnPropertyDescriptor|**Object.getOwnPropertyDescriptor()**%%
is called on the %%/Proxy|Proxy%%.
<example>
var x = { foo: 1 };
var proxy = new Proxy(x, {
getOwnPropertyDescriptor: function(target, name) {
console.log('in getOwnPropertyDescriptor');
return Object.getOwnPropertyDescriptor(target, name);
}
});
console.dir(Object.getOwnPropertyDescriptor(proxy, 'foo'));
</example>
----
instance.has(target : Object, name : String) : Boolean
Called when **name in proxy** is called.
<example>
var x = { foo: 1 };
var proxy = new Proxy(x, {
has: function(target, name) {
console.log('in has');
return name in target;
}
});
console.log('foo' in proxy);
</example>
----
prototype.isExtensible(target : Object) : Boolean
Called when checking to see if an Object is %%/Object#isExtensible|extensible%%.
<example>
var x = { foo: 1 };
var proxy = new Proxy(x, {
isExtensible: function(target) {
console.log('in isExtensible');
return Object.isExtensible(target);
}
});
console.log(Object.isExtensible(proxy));
Object.preventExtensions(proxy);
console.log(Object.isExtensible(proxy));
</example>
----
instance.ownKeys(target : Object) : Array<String>
Called when
%%/Object#keys|**Object.keys()**%%,
%%/Object#getOwnPropertyNames|**Object.getOwnPropertyNames()**%%,
or
%%/Object#getOwnPropertySymbols|**Object.getOwnPropertySymbols()**%%
are called on the proxy.
<example>
var x = { foo: 1 };
Object.defineProperty(x, 'bar', { value: 2,
writable: true,
enumerable: false,
configurable: true } );
var proxy = new Proxy(x, {
ownKeys: function(target, a, b) {
console.log('in ownKeys');
var names = Object.getOwnPropertyNames(target);
var symbols = Object.getOwnPropertySymbols(target);
return names.concat(symbols);
}
});
console.log(Object.keys(proxy));
console.log(Object.getOwnPropertyNames(proxy));
console.log(Object.getOwnPropertySymbols(proxy));
</example>
----
prototype.preventExtensions(target : Object) : Boolean
Called when %%Object#preventExtensions|**Object.preventExtensions()**%%
is called on the %%/Proxy|Proxy%%. Return **true** if
preventExtensions succeeded.
<example>
var x = { foo: 1 };
var proxy = new Proxy(x, {
preventExtensions: function(target) {
console.log('in preventExtensions');
Object.preventExtensions(target);
return !Object.isExtensible(target);
}
});
console.log(Object.isExtensible(proxy));
console.log(Object.preventExtensions(proxy));
console.log(Object.isExtensible(proxy));
</example>
----
instance.set(target : Object, name : String, value : Object, proxy : Proxy) : Object
Called when setting the **name** property of the **target** of the proxy.
<example>
var x = { foo: 1 };
var proxy = new Proxy(x, {
set: function(target, name, value, proxy) {
console.log('in set');
target[name] = value.toUpperCase();
}
});
proxy.foo = 'bar';
console.log(proxy.foo);
console.log(x.foo); // value stored on x is uppercase
</example>