Thursday, August 28, 2014

Example for Closure, .call, .apply in javascript


function add(y,z){ 
function fn(x,y,z){ 
console.log(Math.round(x+y)*z);
};
fn(this.x,y,z);
};

add.call({x: 20.1},3.1,5.1);  // 117.3
add.apply({x: 20.1},[3.1,5.1]);  // 117.3