This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function flatter(arr , res){ | |
if(!Array.isArray(arr)){ | |
return; | |
} | |
for(var elem in arr){ | |
if(Array.isArray(arr[elem])){ | |
flatter(arr[elem], res); | |
}else { | |
res.push(arr[elem]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function flatter (arr) { | |
var clonedList = arr.slice(0) | |
var flatList = [] | |
while (clonedList.length) { | |
var item = clonedList.shift() | |
if (item instanceof Array === true) { | |
clonedList = item.concat(clonedList) | |
} else { | |
flatList.push(item) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var p = new Promise(function(resolve, reject){ | |
var name = 'anoop'; | |
resolve(name); | |
var name = 'error' | |
//reject(name); | |
}) | |
p.then(addLastName) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is like a parameter to a function and it depends on the value binded to it. | |
// Binding of value to 'this' is comptely taken care by language. | |
// The calling object of the function is binded to "this". | |
var obj = { | |
method: function (){ | |
console.log(this); | |
}, | |
param:'value' | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fn = function (a,b){ | |
console.log(this , a , b); | |
} | |
var red = { method : fn }; | |
red.method(1,2); // { method : fn }, 1,2 | |
fn(1,2) // window, 1,2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fn = function(a,b){ | |
console.log(this, a ,b); | |
} | |
setTimeout(fn, 100); // global, undefined , undefinded , assuming fn is called from the implimentation of | |
// setTimeout like cb(); "this" in callbacks completely depends on the way they are called. | |
// as "this" in callbacks completely depends on the way they are called, still we can be sure about "this" in our callbacks. | |
var customThis = " this"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Promises are different then events in these ways- | |
// 1- They are called only once. | |
// 2- we can have simple chaining then nested and complex chaining. | |
// 3- They are wrapped in try catch block | |
// 4- If a promise return any value , that itself becomes then-able. | |
// 5- Promise can be handled once it is already settled unlike eventHandlers we can't handle an event before setting eventHandlers. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var red = {a:1}; | |
var blue = extend({},red); | |
console.log(blue.a); // 1 | |
red.b = 2; | |
console.log(blue.b); // undefined | |
var green = Object.create(red); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Simple copy or deep copy of any object to other is just a copy of available properties of object to the other. | |
// Prototypical copy or chaining of any object is done by Object.create() method. | |
var a = {a:1}; | |
var b = Object.create(a); // now b will have all properties of a and future proprties of a too. | |
// Where as in prototypical chain , child object delegates all the request for the properties to the parent | |
// which are not found in the object. | |
// It works like the inherited parent Object, property lookups are only delegated to parent object only when it is |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var car = function(){ | |
console.log('car'); | |
} | |
car.name = "baleno"; | |
car.obj = {a:1}; | |
car(); // car | |
console.log(car); // function(){console.log('car')}, this is an object actually | |
console.log(car.name) // "baleno" |
OlderNewer