Skip to content
Related Articles

Related Articles

JavaScript Proxy Complete Reference

Improve Article
Save Article
  • Last Updated : 23 Nov, 2022
Improve Article
Save Article

JavaScript Proxy() Object is used to define the custom behavior of fundamental operations (e.g. property lookup, assignment, enumeration, function invocation, etc).

Syntax:

const p = new Proxy(target, {
 Proxy method: function(target, thisArg, argumentsList) {
  }
});

Example: Below examples illustrate the handler.apply() method in JavaScript:

Javascript




<script>
 
    function sum(a, b) {
      return a + b;
    }
      
    const handler = {
      apply: function(target, thisArg, argumentsList) {
        console.log(`Calculate sum: ${argumentsList}`);
      
        return target(argumentsList[0], argumentsList[1])*14/3;
      }
    };
      
    const proxy1 = new Proxy(sum, handler);
      
    console.log(sum(23, 4));
    console.log(proxy1(23, 4));
</script>

Output:

27
"Calculate sum: 23, 4"
126

The complete list of JavaScript Proxy is listed below:

Proxy Methods:

JavaScript Proxy Methods

Description

JavaScript handler.apply() MethodThe handler.apply() method in JavaScript is used as a trap for a function call.
JavaScript handler.construct() MethodThe handler.construct() method in JavaScript is a trap for the new operation and this method returns an object.
JavaScript handler.defineProperty() MethodThe handler.defineProperty() method in Javascript is used to define the new properties and to modify the existing properties directly on an object.
JavaScript handler.deleteProperty() MethodThe handler.deleteProperty() method in JavaScript is a trap for the delete operator.
JavaScript handler.get() MethodThe handler.get() method in JavaScript is a trap for getting a property value.
JavaScript handler.getOwnPropertyDescriptor() MethodThe handler.getOwnPropertyDescriptor() method in Javascript is a trap for Object.getOwnPropertyDescriptor() method.
JavaScript handler.getPrototypeOf() MethodThe handler.getPrototypeOf() method in JavaScript is a trap for the internal method.
JavaScript handler.has() MethodThe handler.has() method in JavaScript is used to “hide” any property that you want.
JavaScript handler.isExtensible() MethodThe handler.isExtensible() method in JavaScript is a trap for Object.isExtensible() method and it returns a boolean value.
JavaScript handler.ownKeys() MethodThe handler.ownKeys() method in JavaScript is a trap for Reflect.ownKeys() method and this method returns an enumerable object.
JavaScript handler.preventExtensions() MethodThe handler.preventExtensions() method in JavaScript is a trap for Object.preventExtensions() method and it returns a boolean value.
JavaScript handler.set() MethodThe handler.set() method in JavaScript is a trap for setting a property value. This method returns a boolean value.
JavaScript handler.setPrototypeOf() MethodThe handler.setPrototypeOf() method in JavaScript is a trap for Object.setPrototypeOf() method and it returns a Boolean value.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!