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) {
...
};

React lifecycle and hooks


Angular project folder structure




“Change Detection”?

Change Detection means updating the view (DOM) when the data has changed.

Change Detection is done in two steps
  1. Update the application model (developer);
  2. Reflect the state of the model in the view (Angular).

If we take a todo application, represented by the graphic above. We have a fairly simple application that has a TodosComponent managing a list of TodoComponent. If we modify this list, by changing the value of the first item of the todos’ list (in yellow), we will have the following flow:
  1. The developer is making changes to the model (like a component’s bindings);
  2. Angular’s change detection kicks in to propagate the changes;
  3. Change detection goes through every components in the component tree (from top to bottom) to check if the model it depends on changed;
  4. If Yes, it will update the component;
  5. Angular updates the component’s view (DOM).
The way Angular runs the change detection by starting from the top and continuing until it reaches the bottom, makes the system more predictable and more performant.



Angular Change Detection Strategies


Angular provides you two Change Detection Strategies, the default one and the onPush.

ChangeDetectionStrategy.Default


By default, Angular has to be conservative and will checks every time something may have changed, this is called dirty checking.

ChangeDetectionStrategy.onPush



With onPush, Angular will only depend on the component’s inputs, events, markForCheck method, or the use of the async pipe in the template, to perform a change detection mechanism and update the view.


Subjects 



In order to understand the difference between a Subject and an Observable, you need to be aware of two distinct concepts
– A data producer
– A data consumer




Example 2

Observable wrapped in a subject, causing shared execution


Angular switch map and merge map
Rx js debounce and total
diff between observable and subject
weekset and set
Angular change detection
array fill





Javascript datatype

Understanding objects - Data Property Attribute
configurable - for delete


Understanding objects - DefineProperty




Array Iteration method : Filter


Understanding Objects



Understanding Objects  - Accessor Property



Understanding Objects  - Accessor Property definedproperty Get and Set



Creating objects





Arrays Introduction




Arrays Introduction  - conversion method (toString())



Array Stack Data structure methods



Array Queue Data structure methods


Array Reordering methods : sort and callback to compare function






Array manipulation methods - Array copy using concat




Array manipulation methods - using Slice and splice




Array Iteration methods : map and foeach


Array Iteration methods: Filter


Array Iteration methods: every and some





Array reduction methods : reduce



Array reduction methods : reduce - nested array to flat array  - find unique char



Date and RegEx Objects




Prototype Inheritance




HTTP Methods


Singleton
Call, Bind, Apply




Promise in Javascript



Custom mybind 
Finding an Object's Size in JavaScript


Array Flat and Array Fill
Closure:
protected, encapsulated
access inner function thorough outer function

3 Ways to Clone Objects in JavaScript



Remove Array Duplicates in ES6




Javascript: Using reduce() to find min and max values?


const ArrayList = [1, 2, 3, 4, 3, 20, 0];
const LargestNum = ArrayList.reduce((prev, curr) => {
      return Math.max(prev, curr)
});
const MinNum = ArrayList.reduce((prev,curr)=>{
      return Math.min(pre,curr)
});
console.log(LargestNum);
console.log(MinNum);

Count number of occurrences for each char in a string

// The string
var str = "I want to count the number of occurances of each char in this string";

// A map (in JavaScript, an object) for the character=>count mappings
var counts = {};

// Misc vars
var ch, index, len, count;

// Loop through the string...
for (index = 0, len = str.length; index < len; ++index) {
    // Get this character
    ch = str.charAt(index); // Not all engines support [] on strings

    // Get the count for it, if we have one; we'll get `undefined` if we
    // don't know this character yet
    count = counts[ch];

    // If we have one, store that count plus one; if not, store one
    // We can rely on `count` being falsey if we haven't seen it before,
    // because we never store falsey numbers in the `counts` object.
    counts[ch] = count ? count + 1 : 1;
}
Shorter answer, with reduce:
let s = 'hello';
[...s].reduce((a, e) => { a[e] = a[e] ? a[e] + 1 : 1; return a }, {}); 
// {h: 1, e: 1, l: 2, o: 1} 

Factory vs Abstract factory design pattern




var mybind = function( fn, b ) {
 return fn.bind(this, b);
 };

var concat = function(a, b) { return a + " " + b;}
var good = mybind(concat, "good");
console.log(good("night"));

newest questions on wordpress