Posts

Showing posts from February, 2017

ES6 Destructuring and Template strings

var obj = { name:"X", surname: "Y" } var { name, surname } = obj; console.log("name", name); console.log("surname", surname); var values = ["Hey", "Hi", "Ssup"]; var [ x, y, z ] = values; console.log("x", x); console.log("y", y); console.log("y", z); // Template strings using backtics var str = `Value of x is ${x} and vallue of y is ${y}`; console.log(str);

Variable assignment in JavaScript

var num1 = 10; var num5 = 40; num1=num2=num3=num4=num5;console.log("Value of num1", num1);console.log("Value of num2", num2);console.log("Value of num3", num3);console.log("Value of num4", num4);console.log("Value of num5", num5);  Question: What is the value of num1, num2, num3, num4 and num5?
Answer: 40 Assignment is done from right to left.

Promise in Javascript

let promiseToFixSomething = new Promise(function(resolve, reject) { //Learning let isFixed = false; if (isFixed ) { resolve('Fixed!!'); } else { reject('Nah'); } }); promiseToFixSomething.then(function(fromResolve) { console.log('Fixed?' + fromResolve); }).catch(function(fromReject){ console.log('Fixed?' + fromReject); });

JS Call, Apply and Bind

.bind() when you want that function to later be called with a certain context( Returns function )
.call() or .apply() when you want to invoke the function immediately ( Returns value )

var object = {num:2}; var myFunction = function(arg1, arg2, arg3){      return this.num + arg1 + arg2 + arg3; }; console.log(myFunction.call(object, 1, 2, 3)); console.log(myFunction.apply(object, [1, 2, 3])); var bound = myFunction.bind(object); console.log(bound(1, 2, 3));