
let proxy = new Proxy(target, handler); let proxyTest = new Proxy({}, {}) if (proxyTest instanceof Object) { document.write("Proxy supported!"); } let obj = { c: "car", b: "bike" }; document.write(obj.b, " "); // -> "bike" document.write(obj.c, " "); // -> "car" document.write(obj.l); // -> "undefined" get interceptor handler. The handler will pass the target object and the requested key to the interceptor. let handler = { get: function(target, name) { return name in target ? target[name] : "Key does not exist"; } } let obj = { c: "car", b: "bike" }; let proxyObj = new Proxy(obj, handler); document.write(proxyObj.b, " "); // -> "bike" document.write(proxyObj.c, " "); // -> "car" document.write(proxyObj.l); // -> "Key does not exist" get interceptor is executed when attempting to access an object property using a proxy. The get method takes the target parameter (the object that needs to be worked through a proxy) and property (the property we are trying to access). var p = new Proxy(target, { get: function(target, property, receiver) { } }); target : target.property : the name of the property to work with.receiver : proxy object or object inherited from the proxy objectget interceptor before displaying it on the screen. let handler = { get: function(target, name) { return name in target ? target[name]*10 : "Key does not exist"; } } let obj = { a: 1, b: 2 }; let proxyObj = new Proxy(obj, handler); document.write(proxyObj.a, " "); // -> 10 document.write(proxyObj.b, " "); // -> 20 document.write(proxyObj.c); // -> "Key does not exist" set interceptor is executed when trying to set an object property using a proxy. The set method accepts the target parameter (the object we are going to access), property (the property we are going to set), and value (the value of the property we are going to set). var p = new Proxy(target, { set: function(target, property, value, receiver) { } }); target : target.property : the name of the property to set.value : the new value of the property of interest.receiver : the object to which the assignment operation was originally directed. Usually this is the proxy itself. However, the set handler may not be called directly, but through a chain of prototypes or in some other way.proxyObj ).proxyObj try to set a new property of an object, the interceptor will be called and the value of the property changed by it will be saved in the object. let handler = { set: function(target, name, value) { target[name] = value*10; } } let obj = { a: 1, b: 2 }; let proxyObj = new Proxy(obj, handler); proxyObj.c = 3; document.write(proxyObj.a, " "); // -> 1 document.write(proxyObj.b, " "); // -> 2 document.write(proxyObj.c); // -> 30 has interceptor is called when the in operator is executed. The has method takes the target (target object) and property (property that you want to control access to using the proxy object). var p = new Proxy(target, { has: function(target, property) { } }); target : target.property : the name of the property whose existence will be checked.ar is in the key. To begin with, we will check if the key exists, and, if so, we will check if it contains the substring of interest. If both conditions are met, we will return the logical value true , otherwise, we will return false . const handler = { has: function(target, key) { if (key in target && key.includes("ar")) { return true; } return false; } }; const user = { car: 'Bentley', bar: 4, hotel: "no", }; const proxy = new Proxy(user, handler); document.write('car' in proxy, " "); // -> true document.write('car' in user, " "); // -> true document.write('hotel' in proxy, " "); // -> false document.write('hotel' in user, " "); // -> true document.write('spacebar' in proxy, " "); // -> false document.write('spacebar' in user); // -> false apply interceptor allows you to call a proxy with parameters. It allows you to override functions. The apply method takes the target parameters (the target object or the target function), thisArg ( this argument to use when calling), and argumentsList (a list of all the arguments in the form of an array). var p = new Proxy(target, { apply: function(target, thisArg, argumentsList) { } }); target : target.thisArg : this argument to call.argumentsList : a list of arguments to invoke. function multiply(a, b) { return a * b; } const handler = { apply: function(target, thisArg, argumentsList) { return target(argumentsList[0], argumentsList[1]) + 1; } }; var proxy = new Proxy(multiply, handler); document.write(multiply(2, 5), " "); // -> 10 document.write(proxy(2, 5)); // -> 11 construct interceptor is executed when the new operator is called. In order for this interceptor to function normally, it is necessary that the target object, to work with which you plan to call it, could be created with a command like new Target() . var p = new Proxy(target, { construct: function(target, argumentsList, newTarget) { } }); target : target.argumentList : argumentList list for the constructor.newTarget : source constructor function formatCurrency(format) { this.format = format; } const handler = { construct: function(target, args) { return new target("$" + args[0]); } }; const proxy = new Proxy(formatCurrency, handler); document.write(new proxy('100').format); // -> $100 deleteProperty interceptor deleteProperty invoked when the delete method is invoked. It takes the target parameters (the target object or the target function), and property (the property whose removal command we want to process). var p = new Proxy(target, { deleteProperty: function(target, property) { } }); target: target.property : the name of the property whose removal operation is to be intervened. const cars = { merc: 's320', buggati: 'veyron', }; const handler = { deleteProperty: function(target, prop) { if (prop in target) { document.write(`${prop} has been removed `); // -> merc has been removed delete target[prop]; } } }; document.write(cars.merc, " "); // -> "s320" const proxy = new Proxy(cars, handler); delete proxy.merc; document.write(cars.merc, " "); // -> undefined 
Source: https://habr.com/ru/post/359060/
All Articles