Javascript call
by Rijo R
HTML
<h3>
Call | Apply | Bind
</h3>
<p>
Function or mrethod can reuse the object using call method.
It is nothing but call to the object
</p>
<p>
Apply is simalr to the call,passing single argument we can pass array of argument to the function
</p>
<p>
Bound an object to the method and it return a function. Then pass argumnet to that function
</p>
JavaScript
var obj = [{
num : 5,
name:"Gopalan",
age:27
}, {
num : 7,
name:"Raju",
age:42
}]
var addFun = function(a) {
console.log(typeof(a));
var c =0;
if(Array.isArray(a)) {
a.forEach(elm=> {
c+=elm;
console.log(elm);
})
}
else { c = a; }
return `Total sum result : ${this.age + c} \n My Name is: ${this.name}`;
}
obj.forEach(element=> {
//console.log(element);
console.log(addFun.call(element, 6));
})
//console.log(addFun.call(obj[0], 5));
var arr = [12,14,10,11];
console.log(addFun.apply(obj[0], [arr]));
var boundFun = addFun.bind(obj[1]);
console.log(boundFun(arr))