ES6 and Beyond

ES6 and Beyond

How about learning some cool features of ES6 and Beyond (like, ES7, ES8).

How about learning some cool features of ES6 and Beyond (like, ES7, ES8).

Hello folks,

ES6, pretty much hyped topic these days in front-end world.

So, how about learning some cool features of ES6 and Beyond (like, ES7, ES8).

First thing first.

What ES means?
ES stands for ECMAScript.
What is the different between ECMAScript and JavaScript then?* ECMAScript is the standard whereas JavaScript is also the standard. But,

  • ECMAScript is specification whereas JavaScript is the implementation of ECMAScript specifications.

So, let’s get started.

What we are going to learn?

  • let
  • const
  • Object Rest/Spread properties
  • Arrow Function
  • String interpolation
  • Exponential **
  • Array.prototype.includes
  • String.prototype.padStart
  • String.prototype.padEnd
  • Object.values
  • Object.entries

Let

let is the new var but the advantage is that it is accessible & available within the scope only. Let’s see an example.

function let_n_var() {
    console.log(foo);
    if(true) {
        let foo = 'foo';
    }
}
let_n_var();
// Output: Uncaught ReferenceError: foo is not defined

In the above example, if you replace let with var, the output would be undefined and there will be no error. So, as you can see, let helps us to catch early errors and no scope mess.

Const

This is again a new way to declare var but the difference is that once a const is defined, we can not re-assign a value to it.

const baz = 'baz';
console.log('baz: ', baz);
// Output: baz
baz = 'baaz'
// Output: Uncaught TypeError: Assignment to constant variable.

As we know that JavaScript is dynamic language and we tend to do mistakes while updating the variables with different data type or data, const helps us to not override the values once set.

Object Rest/Spread properties

Lets look at it by example.

// Rest
const vowels = ['A', 'E', 'I', 'O', 'U'];
const [firstVowel, secondVowel, ...restVowels] = vowels;
console.log(firstVowel); // Output: 'A'
console.log(secondVowel);// Output: 'E'
console.log(restVowels); // Output: ['I', 'O', 'U']

In the above code snippet, I am trying to get the values from variable vowels into separate variables for first two vowel and remaining in rest variable.

Here are the steps to achieve the same.

  • Create a variable with array notation: [firstVowel, secondVowel]. Assign it the vowels variable. This will give us first two vowels in respective variables (based on index)
  • Now, part of third value in array notation, use ... with a variable name. eg. restVowels. So, the variable will look like const [firstVowel, secondVowel, ...restVowels]=vowels;
  • When code executes, it will create variables with values shown above. The alternate way of achieving the same result could be:
const firstVowel = vowels[0];
const secondVowel = vowels[1];
const restVowels = vowels.slice(2); // all remaining values

But, rest approach makes code more readable and manageable.

Here is the code sample for **spread **considering the above code block (used for rest)

// Rest
const vowels = ['A', 'E', 'I', 'O', 'U'];
const [firstVowel, secondVowel, ...restVowels] = vowels;
// Spread
const vowelsWithOneConsonant = ['B', ...vowels];
console.log(vowelsWithOneConsonant); // Output: ['B', 'A', 'E', 'I', 'O', 'U']

As you can see above, it is very easy to spread the variable vowels to create a new variable with same and one added value too.

These same concepts can be applied on objects too. Go ahead and do some practice. It is fun to use once you learn get used to it.

Arrow Function

This is short hand notation of function but the binding of this works differently.

Let’s understand the syntax first.

// standard way of defining function
function getName() {
   console.log('getName function');
}
// converted to arrow function
const getName = () => {
   console.log('getName function');
}

This becomes more interesting and simple when returning values from function.

const getName = (name) => 'Hello' + name;
console.log(getName('Anand')); // Output: Hello Anand

At first glance, it may look confusing but lets break down the code little bit.

  1. We have created a const variable named getName
  2. Assigned a function shorthand used for arrow function () => {//function body}
  3. By default, arrow function do not need any {} brackets if our code do not expect more than one line. Use curly brackets if we need to. Without curly bracket, arrow function returns the executed code line. In the above example, it returns Hello Anand

So, the above code can also be written as shown below and the output will be same.

const getName = (name) => {
     return 'Hello' + name;
}
console.log(getName('Anand')); // Output: Hello Anand

After some practice, you will get it. Go ahead, fire up the browser console and do some practice. An exercise for you is to check how this behaves with arrow function in comparison to the normal function.

String interpolation

Early days in javascript, if we need to create string with some dynamic values, we used to use +. Sooner or later, it gets more confusing if it gets really bigger.

So, the string interpolation came to rescue. The syntax is to use back-tick (`) instead of single/double quotes. And, ${} helps us to put dynamic data without worrying about opening/closing of the sentance or going wrong about missing+ or single/double quotes.

Lets look at the example below:

// Old approach
var guest = 'all';
var name = 'Anand';
var age = 32;
var hobby1= "listening to music";
var hobby2 = "mobiles games";
const sayHello = "Hello " + guest + "! My name is " + name + ". My age is " + age + " years. My hobbies are " + hobby1 + ", "+ hobby2 + '.';
console.log(sayHello); // Output: Hello all! My name is Anand. My age is 32 years. My hobbies are listening to music, mobiles games.

I am sure, it is already looking confusing to you guys. Now look at the below example

// New approach
const guest = 'all';
const name = 'Anand';
const age = 32;
const hobby1= "listening to music";
const hobby2 = "mobiles games";
const sayHello = `Hello ${guest}! My name is ${name}. My age is ${age} years. My hobbies are ${hobby1}, ${hobby2}.`;
console.log(sayHello); // Output: Hello all! My name is Anand. My age is 32 years. My hobbies are listening to music, mobiles games.

Isn’t that cool. 😎

Exponential

I am sure you all know Exponent in mathematics. In Javascript, we used to use Math.pow(5, 6); // Output: 15625 to get the exponent.

Now, we have a short hand to get the same result by doing 3 ** 4; // Output: 15625.

So, we can say following:

Math.pow(5, 6) == 5 ** 6;

Array.prototype.includes

As it’s name suggests, we can use includes to get true or false result value by checking if the value is included or not.

Example:

[1, 2].includes(1); // true
[1, 3].includes(2); // false
var foo = 'foo';
foo.includes('f'); // true
foo.includes('F'); // false
// So, it is case sensitive too. Please take care.

String.prototype.padStart/String.prototype.padEnd

This one is quite interesting. Lets define it first.

padStart and padEnd is used to pad the current string with another string until the given string length reaches.

padStart applies padding from start (left) of the string. On the other hand padEnd applies padding from end (right) of the string.

These functions can apply padding to the current string with another string multiple times as well, if needed.
Let’s have a look to an example

// padStart
const cardNumber = '1234567812345678';
const last3Digit = cardNumber.slice(-3);
const maskedCardNumber = last3Digit.padStart(16, 'X');
console.log(maskedCardNumber); // Output: "XXXXXXXXXXXXX678"
// padEnd
const loremIpsum = "Lorem Ipsum is simply dummy text of the printing and";
const loremIpsumWithDots = loremIpsum.padEnd(loremIpsum.length+3, '.');
console.log(loremIpsumWithDots);
// Output: Lorem Ipsum is simply dummy text of the printing and...

How cool is that? It is pretty interesting and useful. Do practice though.

Object.values

Earlier, we used to use Object.keys if we need to iterate over an object. But now, we can use values or entries instead keys as per our need.

So, with Object.values, it returns all the enumerable properties values as an array. Then, it is easy to consume these values directly.

Example:

const objectFoo = {
   name: 'foo',
   isAvailable: false
};
Object.values(objectFoo);
// Output: ['foo', false]

Object.entries

On the other hand, Object.entries also gives an array but it contains objects’ own enumerable string key property [key, value] pairs. The order remains same as in provided by for...in.

Example:

const objectFoo = {
   name: 'foo',
   isAvailable: false
};
Object.entries(objectFoo);
// Output: [['name', 'foo'], ['isAvailable', false]]

Summary

That’s it for this post.

Posts

1

Thanks for reading ❤

2

If you liked this post, share it with all of your programming buddies!

3

Follow me on **[Facebook]( https://www.facebook.com/moriohdotcom)** | **[Twitter]( https://twitter.com/moriohdotcom)**

4

5

### Learn More

6

7

☞ [The Complete JavaScript Course 2019: Build Real Projects!](http://learnstartup.net/p/rJWeUz65Z "The Complete JavaScript Course 2019: Build Real Projects!") 

8

☞ [Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)](http://learnstartup.net/p/BJBa7-l-g "Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)")

9

☞ [JavaScript Bootcamp - Build Real World Applications](http://learnstartup.net/p/-J13WdymB "JavaScript Bootcamp - Build Real World Applications") 

10

☞ [The Web Developer Bootcamp](https://learnstartup.net/p/SJQU6Gxbg "")

11

☞ [JavaScript: Understanding the Weird Parts](http://learnstartup.net/p/HyUQWUMTqW "JavaScript: Understanding the Weird Parts")

12

☞ [ES6 Cool stuffs — A big fat Arrow](http://tech.learn4startup.com/15a776baf1 "")

13

☞ [Dropbox API / JavaScript ES6 Tutorial - Expense Organizer](http://tech.learn4startup.com/76794978c8 "")

14

☞ [ES6 Arrow Functions Cheatsheet](http://dev.edupioneer.net/4c6ec0b2ec "")

15

☞ [Modern JS with ES6](http://dev.edupioneer.net/b6cffce368 "")

16

☞ [These are the features in ES6 that you should know](http://dev.edupioneer.net/0b33b15561 "")

17

☞ [Get started with Next-Gen Javascript](http://dev.edupioneer.net/acd070a7a3 "")

18

19

*Originally published on [https://hackernoon.com](https://hackernoon.com/es6-and-beyond-28aed8574e00)*

Linked Lists in JavaScript With ES6

Linked Lists in JavaScript With ES6

Linked Lists in JavaScript with ES6.This series is about data structure implementation in JavaScript using the ES6 specification.. Let's see how to make a singly linked list in Javascript… ... list in Javascript. We'll be using ES6 syntax throughout.

This is a continuation of a previous piece where we digested all surrounding concepts, pros and cons, Big O time complexity, real use cases, linked-list mainly operations, and all that kind of theory. If you have not read it yet, I recommend you read it first.

This series is about data structure implementation in JavaScript using the ES6 specification.

The aim of this second piece is to walk through the implementation of a linked list. Actually, the two pieces enclose a linked list itself since the prior piece is pointing to this one.

The Node Class

In the next code, we’re going to define our Node class with its constructor. Remember, the node is the basic building block to store the data and the next pointer.

This class will have to handle the node creation. Every time the class is instantiated, the constructor has the responsibility to initialize the two properties: data and next.


Node Class

Now the challenge is to create the next four nodes (just nodes creation, not how to connect them).


Linked List

Basically, we have to instantiate the Node class four times in order to create the four nodes.


Creating Nodes

At this point, we don’t care about the second parameter. Why? Because at this moment, we’re just learning how to create the node without having to worry about how they’ll be connecting together.

How Can We Connect the Nodes?

In the prior code, we just created nodes independently. Now is time to learn how to connect them to form the linked list.


Connecting nodes

We have defined the Node class. Next is to define a new class that will handle the nextpointer property and the main operations in the linked list. Let’s create the LinkedList class.


Linked List Class

In the above code, we have just defined a class called LinkedList with its constructor. This has the work of initializing the headproperty, to store the first node,and size, to keep track of the size of the linked list.

Next is to offer the ability to insert to the head, to the tail, or at any random position in the list.

Inserting Into the Head


Inserting to head

We have just created a simple method to add nodes to the head of the linked list. We are passing down to it the dataparameter and setting a value for the this.head property creating a new instance of the Node class.

Let’s do some tests of its implementation so far and see the results.

The output will be:


Linked list output

Inserting at the Tail

We just learned how to add nodes to the head. It’s time to know how to add nodes to the tail.


Inserting at the tail

In the aboveinsertToTail function, we are passing down the data parameter, and then we created a new instance of the Node class. After that, we are checking if the head is empty. If so, the head itself will be set to the new node we have just after created. Otherwise, set the tail with the head and then loop through the linked list to find the tail and update the tail’s next pointer.

Inserting at Random Position

Finally, we are going to see how to insert a new node in the linked list at a given random position. For this, we have to traverse the list until we find the desired position.

Inserting at a given random position

Now we are going to test this function using the next tests.

The output will be as below. As you can see, at the given index, the node (600) was added at the second index of the list.

The Whole Code

class Node {
  constructor(data, next = null) {
    this.data = data;
    this.next = next;
  }
}

//Let's create four nodes
let node1 = new Node(5);
let node2 = new Node(10);
let node3 = new Node(20);
let node4 = new Node(1);

//connecting nodes
node1.next = node2;
node2.next = node3;
node3.next = node4;

//LinkedList Class
class LinkedList {
  constructor() {
    this.head = null; //first node of the Linked List
    this.size = 0; //Track size of the linked list
  }
  //Insert to head
  insertToHead(data) {
    this.head = new Node(data, this.head);
    this.size++;
  }

  //Insert into the tail
  insertToTail(data) {
    const node = new Node(data);
    let tail = null;
    //if empty, make it head
    if (!this.head) {
      this.head = node;
    } else {
      tail = this.head;
      while (tail.next) {
        tail = tail.next;
      }
      tail.next = node;
    }
    this.size++;
  }

  //Insert at random position
  insertAt(data, index) {
    //if it's empty
    if (!this.head) {
      this.head = new Node(data);
      return;
    }
    //if it needs add to the front of the list
    if (index === 0) {
      this.insertToHead(data); //reuse insertToHead function
      return;
    }
    let node = new Node(data);
    let current, previous;
    let count = 0;
    // current will be first
    current = this.head;
    while (count < index) {
      previous = current;
      count++;
      current = current.next;
    }
    node.next = current;
    previous.next = node;
    this.size++;
  }
}

const linkedList = new LinkedList();
linkedList.insertToHead(100);
linkedList.insertToHead(200);
linkedList.insertToHead(300);
linkedList.insertToTail(400);
linkedList.insertAt(600, 2);

console.table(linkedList);

LinkedList.js

I hope you have gained more knowledge about data structure and especially with Linked list. That’s all for now.

Thanks for reading!

An Introduction to JavaScript ES6 Proxies

An Introduction to JavaScript ES6 Proxies

Proxy is one of the most overlooked concepts introduced in ES6 version of JavaScript, but ES6 proxies bound to come in handy at some point in your future.

Proxy is one of the most overlooked concepts introduced in the ES6 version of JavaScript.

Admittedly, it isn’t particularly useful on a day-to-day basis, but it is bound to come in handy at some point in your future.

The basics

The Proxy object is used to define a custom behavior for fundamental operations such as property lookup, assignment, and function invocation.

The most basic example of a proxy would be:

const obj = {
 a: 1,
 b: 2,
};

const proxiedObj = new Proxy(obj, {
 get: (target, propertyName) => {
   // get the value from the "original" object
   const value = target[propertyName];

   if (!value && value !== 0) {
     console.warn('Trying to get non-existing property!');

     return 0;
   }

   // return the incremented value
   return value + 1;
 },
 set: (target, key, value) => {
   // decrement each value before saving
   target[key] = value - 1;

   // return true to indicate successful operation
   return true;
 },
});

proxiedObj.a = 5;

console.log(proxiedObj.a); // -> incremented obj.a (5)
console.log(obj.a); // -> 4

console.log(proxiedObj.c); // -> 0, logs the warning (the c property doesn't exist)

We have intercepted the default behavior of both get and set operations by defining the handlers with their respective names in the object provided to the proxy constructor. Now each get operation will return the incremented value of the property, while set will decrement the value before saving it in the target object.

What’s important to remember with proxies is that once a proxy is created, it should be the only way to interact with the object.

Different kinds of traps

There are many traps (handlers that intercept the object’s default behavior) aside from get and set, but we won’t be using any of them in this article. With that being said, if you are interested in reading more about them, here’s the documentation.

Having fun

Now that we know how proxies work, let’s have some fun with them.

Observing object’s state

As it has been stated before it is very easy to intercept operations with proxies. To observe an object’s state is to be notified every time there’s an assignment operation.

const observe = (object, callback) => {
 return new Proxy(object, {
   set(target, propKey, value) {
     const oldValue = target[propKey];
   
     target[propKey] = value;

     callback({
       property: propKey,
       newValue: value,
       oldValue,
     });

     return true;
   }
 });
};

const a = observe({ b: 1 }, arg => {
 console.log(arg);
});

a.b = 5; // -> logs from the provided callback: {property: "b", oldValue: 1, newValue: 5}

And that’s all we have to do — invoke the provided callback every time the set handler is fired.

As an argument to the callback, we provide an object with three properties: the name of the changed property, the old value, and the new value.

Prior to executing the callback, we assign the new value in the target object so the assignment actually takes place. We have to return true to indicate that the operation has been successful; otherwise, it would throw a TypeError.

Here’s a live example.

Validating properties on set

If you think about it, proxies are a good place to implement validation — they are not tightly coupled with the data itself. Let’s implement a simple validation proxy.

As in the previous example, we have to intercept the set operation. We would like to end up with the following way of declaring data validation:

const personWithValidation = withValidation(person, {
 firstName: [validators.string.isString(), validators.string.longerThan(3)],
 lastName: [validators.string.isString(), validators.string.longerThan(7)],
 age: [validators.number.isNumber(), validators.number.greaterThan(0)]
});

In order to achieve this, we define the withValidation function like so:

const withValidation = (object, schema) => {
 return new Proxy(object, {
   set: (target, key, value) => {
     const validators = schema[key];

     if (!validators || !validators.length) {
       target[key] = value;

       return true;
     }

     const shouldSet = validators.every(validator => validator(value));

     if (!shouldSet) {
       // or get some custom error
       return false;
     }

     target[key] = value;
     return true;
   }
 });
};

First we check whether or not there are validators in the provided schema for the property that is currently being assigned — if there aren’t, there is nothing to validate and we simply assign the value.

If there are indeed validators defined for the property, we assert that all of them return true before assigning. Should one of the validators return false, the whole set operation returns false, causing the proxy to throw an error.

The last thing to do is to create the validators object.

const validators = {
 number: {
   greaterThan: expectedValue => {
     return value => {
       return value > expectedValue;
     };
   },
   isNumber: () => {
     return value => {
       return Number(value) === value;
     };
   }
 },
 string: {
   longerThan: expectedLength => {
     return value => {
       return value.length > expectedLength;
     };
   },
   isString: () => {
     return value => {
       return String(value) === value;
     };
   }
 }
};

The validators object contains validation functions grouped by the type they should validate. Each validator on invocation takes the necessary arguments, like validators.number.greaterThan(0), and returns a function. The validation happens in the returned function.

We could extend the validation with all kinds of amazing features, such as virtual fields or throwing errors from inside the validator to indicate what went wrong, but that would make the code less readable and is outside the scope of this article.

Here’s a live example.

Making code lazy

For the final — and hopefully most interesting — example, let’s create a proxy that makes all the operations lazy.

Here’s a very simple class called Calculator, which contains a few basic arithmetic operations.

class Calculator {
 add(a, b) {
   return a + b;
 }

 subtract(a, b) {
   return a - b;
 }

 multiply(a, b) {
   return a * b;
 }

 divide(a, b) {
   return a / b;
 }
}

Now normally, if we ran the following line:

new Calculator().add(1, 5) // -> 6

The result would be 6.

The code is executed on the spot. What we would like is to have the code wait for the signal to be run, like a run method. This way the operation will be postponed until it is needed — or not executed at all if there is never a need.

So the following code, instead of 6, would return the instance of the Calculator class itself:

lazyCalculator.add(1, 5) // -> Calculator {}

Which would give us another nice feature: method chaining.

lazyCalculator.add(1, 5).divide(10, 10).run() // -> 1

The problem with that approach is that in divide, we have no clue of what the result of add is, which makes it kind of useless. Since we control the arguments, we can easily provide a way to make the result available through a previously defined variable — $, for example.

lazyCalculator.add(5, 10).subtract($, 5).multiply($, 10).run(); // -> 100

$ here is just a constant Symbol. During execution, we dynamically replace it with the result returned from the previous method.

const $ = Symbol('RESULT_ARGUMENT');

Now that we have a fair understanding of what do we want to implement, let’s get right to it.

Let’s create a function called lazify. The function creates a proxy that intercepts the get operation.

function lazify(instance) {
 const operations = [];

 const proxy = new Proxy(instance, {
   get(target, propKey) {
     const propertyOrMethod = target[propKey];

     if (!propertyOrMethod) {
       throw new Error('No property found.');
     }

     // is not a function
     if (typeof propertyOrMethod !== 'function') {
       return target[propKey];
     }

     return (...args) => {
       operations.push(internalResult => {
         return propertyOrMethod.apply(
           target,
           [...args].map(arg => (arg === $ ? internalResult : arg))
         );
       });

       return proxy;
     };
   }
 });

 return proxy;
}

Inside the get trap, we check whether or not the requested property exists; if it doesn’t, we throw an error. If the property is not a function, we return it without doing anything.

Proxies don’t have a way of intercepting method calls. Instead, they are treating them as two operations: the get operation and a function invocation. Our get handler has to act accordingly.

Now that we are sure the property is a function, we return our own function, which acts as a wrapper. When the wrapper function is executed, it adds yet another new function to the operations array. The wrapper function has to return the proxy to make it possible to chain methods.

Inside the function provided to the operations array, we execute the method with the arguments provided to the wrapper. The function is going to be called with the result argument, allowing us to replace all the $ with the result returned from the previous method.

This way we delay the execution until requested.

Now that we have built the underlying mechanism to store the operations, we need to add a way to run the functions — the .run() method.

This is fairly easy to do. All we have to do is check whether the requested property name equals run. If it does, we return a wrapper function (since run acts as a method). Inside the wrapper, we execute all the functions from the operations array.

The final code looks like this:

const executeOperations = (operations, args) => {
 return operations.reduce((args, method) => {
   return [method(...args)];
 }, args);
};

const $ = Symbol('RESULT_ARGUMENT');

function lazify(instance) {
 const operations = [];

 const proxy = new Proxy(instance, {
   get(target, propKey) {
     const propertyOrMethod = target[propKey];

     if (propKey === 'run') {
       return (...args) => {
         return executeOperations(operations, args)[0];
       };
     }

     if (!propertyOrMethod) {
       throw new Error('No property found.');
     }

     // is not a function
     if (typeof propertyOrMethod !== 'function') {
       return target[propKey];
     }

     return (...args) => {
       operations.push(internalResult => {
         return propertyOrMethod.apply(
           target,
           [...args].map(arg => (arg === $ ? internalResult : arg))
         );
       });

       return proxy;
     };
   }
 });

 return proxy;
}

The executeOperations function takes an array of functions and executes them one by one, passing the result of the previous one to the invocation of the next one.

And now for the final example:

const lazyCalculator = lazify(new Calculator());

const a = lazyCalculator
 .add(5, 10)
 .subtract($, 5)
 .multiply($, 10);

console.log(a.run()); // -> 100

If you are interested in adding more functionality I have added a few more features to the lazify function — asynchronous execution, custom method names, and a possibility to add custom functions through the .chain() method. Both versions of the lazify function are available in the live example.

Summary

Now that you have seen proxies in action, I hope that you could find a good use for them in your own codebase.

Proxies have many more interesting uses than those covered here, such as implementing negative indices and catching all the nonexistent properties in an object. Be careful, though: proxies are a bad choice when performance is an important factor.

Metaprogramming: An Introduction to JavaScript(ES6) Proxy

Metaprogramming: An Introduction to JavaScript(ES6) Proxy

Metaprogramming: An Introduction to JavaScript(ES6) Proxy - The concept of Metaprogramming is not new. There are many programming languages like, Lisp, Scala, Clojure, Rust, Haskell, etc already got the use of it. JavaScript is not really behind either!

Originally published by Tapas Adhikary at blog.greenroots.info

Before we go any further, let us understand, What is Metaprogramming?

Metaprogramming

Metaprogramming is nothing less than a Magic! Truly, how about writing a program to Read, Modify, Analyze and even to Generate a Program? Doesn't it sound Wizardry and Powerful?

Wikipedia defines Metaprogramming as,

Metaprogramming is a programming technique in which computer programs have the ability to treat other programs as their data.

So basically, it is the Program that deals with the Meta Data of another program and able to do lot of useful things.

Meet Proxy

Proxy wraps objects and intercepts their behavior through traps

Among several ways we can do Metaprogramming in JavaScript, usage of Proxyobject is one of the important one. Proxy object is an ES6 concept used to define custom behavior for fundamental operations (e.g. property lookup, assignment, enumeration, function invocation, etc).

Here are few useful terms you need to remember and use:

  • target: an Object which the proxy virtualizes.
  • handler: a Placeholder Object which contains traps.
  • trap: the Methods that provide property access of the target object.

It is perfectly fine, if you haven't got much from the description above. We will understand it very easily through code and examples.

Code Time

Here is the syntax for creating a Proxy Object:

let p = new Proxy(target, handler); 

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

const employee = {
    firstName: 'Tapas',
    lastName: 'Adhikary'
};
 
console.group('employee');
    console.log(employee.firstName);
    console.log(employee.lastName);
    console.log(employee.org);
    console.log(employee.fullName);
console.groupEnd()

Well, we know the expected output would be,

employee
  Tapas
  Adhikary
  undefined
  undefined

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

  • Step 1: Create a Handler that uses a Trap

We will be using a trap called get which is a trap for getting a property value. Here is our Handler:

let handler = {
    get: function(target, fieldName) {       
 
        if(fieldName === 'fullName' ) {
            return `${target.firstName} ${target.lastName}`;
        }
 
        return fieldName in target ?
            target[fieldName] :
                `No such property as, '${fieldName}'!`
 
    }
};

The above handler helps to create the value for fullName property. It also adds a better error message in case, we are dealing with a missing property.

  • Step 2: Create a Proxy Object

As we have the target as employee object and the handler, we will be able to create a Proxy object as:

let p = new Proxy(employee, handler);
  • Step 3: Access the properties on the Proxy object


console.group('proxy');
    console.log(p.firstName);
    console.log(p.lastName);
    console.log(p.org);
    console.log(p.fullName);
console.groupEnd()

You should be seeing the output as,

proxy
  Tapas
  Adhikary
  No such property as, 'org'!
  Tapas Adhikary

Notice how we have magically changed things for the employee object.

In Previous example, we used a trap called get. Here are the list of available traps:

  • apply
  • construct
  • defineProperty
  • deleteProperty
  • get
  • getOwnPropertyDescriptor
  • getPrototypeOf
  • has
  • isExtensible
  • ownKeys
  • preventExtensions
  • set
  • setPrototypeOf

More on these can be found here, Proxy - JavaScript | MDN

Proxy for Validation of Values

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

const validator = {
    set: function(obj, prop, value) {
        if (prop === 'age') {
            if(!Number.isInteger(value)) {
                throw new TypeError('Age is always an Integer, Please Correct it!');
            }
            if(value < 0) {
                throw new TypeError('This is insane, a negative age?');
            }
        }
    }
};

Again, we can create a Proxy object as:

let p = new Proxy(employee, validator); 

If you do,

p.age = 'I am testing the blunder'; 

The output would be a TypeError as,

TypeError: Age is always an Integer, Please Correct it!
    at Object.set (E:\Projects\KOSS\metaprogramming\js-mtprog\proxy\userSetProxy.js:28:23)
    at Object.<anonymous> (E:\Projects\KOSS\metaprogramming\js-mtprog\proxy\userSetProxy.js:40:7)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:188:16)
    at bootstrap_node.js:609:3

Similarly, try doing this!

p.age = -1;

Use-cases

Proxy Object is a very powerful concept. There are several use-cases where this concept can be used. Here are few:

  • Protect ID field from deletion from an Object(trap: deleteProperty)
  • Tracing Property Accesses(trap: get, set)
  • Data Binding(trap: set)
  • Revocable references
  • Manipulate the in operator behavior

... and many many more.

Last Note

Hope you liked the concept of Proxy Object. Try it out, it is Fun! Feel free to access the examples from My Github Repo.

'Proxy' is not the only concept for JavaScript based Metaprogramming, there are are others like, Reflect. That is coming soon.

Originally published by Tapas Adhikary at blog.greenroots.info

===========================================

Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter

Learn More

☞ Svelte.js - The Complete Guide

☞ The Complete JavaScript Course 2019: Build Real Projects!

☞ Become a JavaScript developer - Learn (React, Node,Angular)

☞ JavaScript: Understanding the Weird Parts

☞ JavaScript: Coding Challenges Bootcamp - 2019

☞ The Complete Node.js Developer Course (3rd Edition)

☞ Angular & NodeJS - The MEAN Stack Guide

☞ NodeJS - The Complete Guide (incl. MVC, REST APIs, GraphQL)

☞ Node.js Absolute Beginners Guide - Learn Node From Scratch