Enables custom behavior for an object.
Syntax
proxyObj = new Proxy(target, handler)
Parameters
target
Required. An object or function to be virtualized by the proxy.
handler
Required. An object with methods (traps) that implement the custom behavior.
Remarks
A Proxy object is used to intercept internal low-level operations on another object. Proxy objects can be used for interception, object virtualization, logging/profiling, and other purposes.
If a trap for a specific operation has not been defined in the handler for the proxy, the operation is forwarded to the target.
The handler object defines the following methods (traps) to implement custom behavior. The examples here are not exhaustive. To support conditional default behavior in the handler method, use methods of Reflect Object.
| Handler method (trap) syntax | Examples of usage |
|---|---|
apply: function(target, thisArg, args) |
A trap for a function call. |
construct: function(target, args) |
A trap for a constructor. |
defineProperty: function(target, propertyName, descriptor) |
A trap for Object.defineProperty Function. |
deleteProperty: function(target, propertyName) |
A trap for the delete statement. |
enumerate: function(target) |
A trap for the for...in statement, Object.getOwnPropertySymbols, Object.keys function, and JSON.stringify. |
get: function(target, propertyName, receiver) |
A trap for any getter properties. |
getOwnPropertyDescriptor: function(target, propertyName) |
A trap for Object.getOwnPropertyDescriptor Function. |
getPrototypeOf: function(target) |
A trap for Object.getPrototypeOf Function. |
has: function(target, propertyName) |
A trap for the in operator, hasOwnProperty Method (Object), and other methods. |
isExtensible: function(target) |
A trap for Object.isExtensible Function. |
ownKeys: function(target) |
A trap for Object.getOwnPropertyNames Function. |
preventExtensions: function(target) |
A trap for Object.preventExtensions Function. |
set: function(target, propertyName, value, receiver) |
A trap for any setter properties. |
setPrototypeOf: function(target, prototype) |
A trap for Object.setPrototypeOf. |
Example
The following code example shows how to create a proxy for an object literal using the get trap.
var target = {};
var handler = {
get: function (receiver, name) {
// This example includes a template string.
return `Hello, ${name}!`;
}
};
var p = new Proxy(target, handler);
console.log(p.world);
// Output:
// Hello, world!
Example
The following code example shows how to create a proxy for a function using the apply trap.
var target = function () { return 'I am the target'; };
var handler = {
// This example includes a rest parameter.
apply: function (receiver, ...args) {
return 'I am the proxy';
}
};
var p = new Proxy(target, handler);
console.log(target()):
console.log(p()):
// Output:
// I am the target
// I am the proxy
Requirements
Supported in Microsoft Edge (Edge browser). Also supported in Store apps (Microsoft Edge on Windows 10). See Version Information.
Not supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards. Not supported in Windows 8.1.


