1.

What are Proxy in ES6?

Answer»

The proxy objects are used to customize behaviour for BASIC operations like property lookup, ASSIGNMENT, enumeration, function invocation, etc.

For basic ACTIONS, the Proxy object is used to create custom behaviour (e.g. property lookup, assignment, enumeration, function invocation, etc).

We need to define three crucial terms:

  • handler —  a placeholder object that holds the trap(s)
  • traps — the method(s) that let you access a property.
  • target — the virtualized object by the proxy

Example:

 const handle = { get: function(ob, prp) { return prp in ob ? ob[prp] : 37; }};const x = new Proxy({}, handle);x.a = 2;x.b = undefined;console.log(x.a, x.b); // 2, undefinedconsole.log('c' in x, x.c); // false, 37

Proxies have a WIDE RANGE of real-world applications.

  • validation
  • correction in value
  • extensions for property lookup
  • property accesses are being tracked
  • references that can be revoked
  • implementation of the DOM in javascript


Discussion

No Comment Found