Adsense

ES6 features

 Arrow Functions
Classes
Enhanced Object Literals
String interpolation
Destructuring
Default
Spread
Spread + Object Literals
Rest
Let
Const
For..of
Unicode
Modules & Module Loaders
Set
WeakSet
Map
WeakMap
Proxies
Symbols
Inheritable Built-ins
New Library
Binary and Octal
Promises
Reflect
Tail Call Optimization

Map

Maps are a store for key / value pairs. Key and value could be a primitives or object references.
Let’s create a map:
let map = new Map(),
    val2 = 'val2',
    val3 = {
      key: 'value'
    };map.set(0, 'val1');
map.set('1', val2);
map.set({ key: 2 }, val3);console.log(map); // Map {0 => 'val1', '1' => 'val2', Object {key: 2} => Object {key: 'value'}}

Set

It’s a collection for unique values. The values could be also a primitives or object references.
let set = new Set();set.add(1);
set.add('1');
set.add({ key: 'value' });console.log(set); // Set {1, '1', Object {key: 'value'}}

WeakMap

WeakMaps provides leak-free object keyed side tables. It’s a Map that doesn’t prevent its keys from being garbage-collected. We don’t have to worry about memory leaks.
If the object is destroyed, the garbage collector removes an entry from the WeakMap and frees memory.
Keys must be objects.
It has almost the same API like a Map, but we can’t iterate over the WeakMap collection. We can’t even determine the length of the collection because we don’t have size attribute here.
The API looks like this:
new WeakMap([iterable])WeakMap.prototype.get(key)        : any
WeakMap.prototype.set(key, value) : this
WeakMap.prototype.has(key)        : boolean
WeakMap.prototype.delete(key)     : boolean

let wm = new WeakMap(),
    obj = {
      key1: {
        k: 'v1'
      },
      key2: {
        k: 'v2'
      }
   };wm.set(obj.key1, 'val1');
wm.set(obj.key2, 'val2');console.log(wm); // WeakMap {Object {k: 'v1'} => 'val1', Object {k: 'v2'} => 'val2'}
console.log(wm.has(obj.key1)); // truedelete obj.key1;console.log(wm.has(obj.key1)); // false

WeakSet

Like a WeakMap, WeakSet is a Seat that doesn’t prevent its values from being garbage-collected. It has simpler API than WeakMap, because has only three methods:
new WeakSet([iterable])WeakSet.prototype.add(value)    : any
WeakSet.prototype.has(value)    : boolean
WeakSet.prototype.delete(value) : boolean

WeakSets are collections of unique objects only.
WeakSet collection can‘t be iterated and we cannot determine its size.
let ws = new WeakSet(),
    obj = {
      key1: {
        k: 'v1'
      },
      key2: {
        k: 'v2'
      }
   };ws.add(obj.key1);
ws.add(obj.key2);console.log(ws); // WeakSet {Object {k: 'v1'}, Object {k: 'v2'}}
console.log(ws.has(obj.key1)); // truedelete obj.key1;console.log(ws.has(obj.key1)); // false
test

Generators

Functions in JavaScript, as we all know, “run until return/end”. Generator Functions on the other hand, “run until yield/return/end”. Unlike the normal functions Generator Functions once called, returns the Generator Object, which holds the entire Generator Iterable that can be iterated using next() method or for…of loop.
Every next() call on the generator executes every line of code until the next yield it encounters and suspends its execution temporarily.


1. Default Parameters in ES6


var calculateArea = function(height = 50, width = 80) {
// write logic
...
}


2. Template Literals in ES6


var name = `Your name is ${firstName} ${lastName}.`


3. Multi-line Strings in ES6


let poemData = `Johny Johny Yes Papa,
Eating sugar? No, papa!
Telling lies? No, papa!
Open your mouth Ah, ah, ah!`


4. Destructuring Assignment in ES6


var o = {p: 42, q: true};
var {p, q} = o;

console.log(p); // 42
console.log(q); // true



6. Arrow Functions in ES6


The fat arrows are amazing because they would make your this behave properly, i.e., this will have the same value as in the context of the function— it won’t mutate.

$('.btn').click((event) => {
this.doSomething()
});


7. Promises in ES6


var asyncCall = new Promise((resolve, reject) => {
// do something async
resolve();
}).then(()=> {
console.log('Yay!');
})



8. Block-Scoped Constructs Let and Const

function calculateAmount(boolVal) { 
   let amount = 0;   if(boolVal) {
      let amount = 1; // scope of this amount ends with next closing bracket
   }   return amount;
}
console.log(calculateAmount(true)); // output: 0

9. Classes in ES6

class Profile {   
   constructor(firstName, lastName = '') { // class constructor
      this.firstName = firstName;
      this.lastName = lastName;     
   }  
    
   getName() { // class method       
     console.log(`Name: ${this.firstName} ${this.lastName}`);    
   } 
}let profileObj = new Profile('Kavisha', 'Talsania');
profileObj.getName(); // output: Name: Kavisha Talsania

10. Modules in ES6

In ES6, there are modules with import and export operands.

export var userID = 10;
export function getName(name) {
...
};

newest questions on wordpress