| title | ms.custom | ms.date | ms.prod | ms.reviewer | ms.suite | ms.technology | ms.tgt_pltfrm | ms.topic | dev_langs | ms.assetid | caps.latest.revision | author | ms.author | manager | ||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Proxy Object (JavaScript) | Microsoft Docs |
01/18/2017 |
windows-client-threshold |
|
language-reference |
|
2b89abee-04fa-47e6-9676-980016cff5f8 |
8 |
mikejo5000 |
mikejo |
ghogen |
Enables custom behavior for an object.
proxyObj = new Proxy(target, handler)
target
Required. An object or function to be virtualized by the proxy.
handler
Required. An object with methods (traps) that implement the custom behavior.
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. |
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!
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 [!INCLUDEjsv12]