Thursday, 8 March 2018

Explain call, apply and bind in OOJ


Examples:-

var obj={ num: 2};
var obj1={ num: 5};

var addNum = function(a,b,c){
   return this.num + a+ b +c;         
};


// by call() method associated of two objects
console.log(addNum.call(obj,1,2,3)); //8

// by apply() method
var arr = [1, 2, 3];
console.log(addNum.apply(obj,arr)); //8
console.log(addNum.apply(obj1,arr)); //11


// by bind() method

vr callbybind = addNum.bind(obj);
console.log(callbybind(3,4,5)); //14












aaa

No comments:

Post a Comment