Skip to content

Instantly share code, notes, and snippets.

View optimistanoop's full-sized avatar
💭
<tod-fod-code>

Anoop Kumar optimistanoop

💭
<tod-fod-code>
View GitHub Profile
@optimistanoop
optimistanoop / app.js
Created July 20, 2016 09:03
Array flatter using recursion.
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]);
}
@optimistanoop
optimistanoop / app.js
Created July 20, 2016 09:10
Array flatter without recursion.
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)
}
@optimistanoop
optimistanoop / app.js
Created November 6, 2016 09:13
Js Promise chaining
var p = new Promise(function(resolve, reject){
var name = 'anoop';
resolve(name);
var name = 'error'
//reject(name);
})
p.then(addLastName)
@optimistanoop
optimistanoop / app.js
Created January 13, 2017 18:33
this in Js
// 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'
};
@optimistanoop
optimistanoop / app.js
Created January 13, 2017 19:08
Example of this in Js
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
@optimistanoop
optimistanoop / app.js
Created January 13, 2017 19:15
this in Js callbacks
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";
@optimistanoop
optimistanoop / app.js
Last active January 13, 2017 19:17
Promise in Js
// 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.
@optimistanoop
optimistanoop / app.js
Created January 14, 2017 06:57
Prototype chains example in Js
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);
@optimistanoop
optimistanoop / app.js
Created January 14, 2017 07:10
Prototype chain in Js
// 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
@optimistanoop
optimistanoop / app.js
Created January 14, 2017 07:30
Special behaviour of Js functions, we can attach properties to the function object
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"