ES6 - Proxy API


Advertisements

ES6 implements intercession form of meta programming using Proxies. Similar to ReflectAPI, the Proxy API is another way of implementing meta programming in ES6. The Proxy object is used to define custom behavior for fundamental operations. A proxy object performs some operations on behalf of the real object.

The various terminologies related to ES6 proxies are given below

Sr.No Method & Description
1

handler

Placeholder object which contains traps

2

traps

The methods that provide property access. This is analogous to the concept of traps in operating systems

1

target

Object which the proxy virtualizes. It is often used as storage backend for the proxy.

Syntax

The syntax stated below is for the Proxy API, where, target can be any sort of object like array, function or another proxy and handler is an object whose properties are functions. This defines the behavior of the proxy.

const proxy = new Proxy(target,handler)

Handler Methods

The handler object contains traps for Proxy. All traps are optional. If a trap has not been defined, the default behavior is to forward the operation to the target. Some common handler methods are as follows −

Sr.No Method & Description
1 handler.apply()

A trap for a function call.

2 handler.construct()

A trap for the new operator.

3 handler.get()

A trap for getting property values.

4 handler.set()

A trap for setting property values.

5 handler.has()

TA trap for the in operator.

Advertisements