Metaprogramming: An Introduction to JavaScript(ES6) Proxy by Tapas Adhikary

Metaprogramming: An Introduction to JavaScript(ES6) Proxy by Tapas Adhikary

Proxy object is an ES6 concept used to define custom behavior for fundamental operations (e.g. property lookup, assignment, enumeration, function invocation, etc). Here is the syntax for creating a Proxy Object:

Now let us take an example of an object and try to print some of the properties of it:

Well, we know the expected output would be,

Now let us use the object to alter this program of handling and provide some behavior to it:

We will be using a trap called which is a trap for getting a property value. Here are the list of available traps:

More on these can be found here, Proxy – JavaScript | MSDN

Let’s create a handler(we can name it as, validator):

Again, we can create a object as:

If you do,

The output would be a as,

Similarly, try doing this!

Source: blog.greenroots.info

Leave a Reply

Your email address will not be published. Required fields are marked *