Call, Apply & Bind
Detail example of call, apply and bind usage in Javascript
JavaScript
(function add(){
alert("hi")
})()
var obj={
name:"Samar",
age: 21
}
//Call Example
function sayCall(add,methodType){
alert("Message from call method " + this.name + " is "+ this.age +" years old and he stays at "+ add + " ,Odisha and Method type is "+ methodType);
}
sayCall.call(obj,"Balasore","Call");
//Apply Example
function sayApply(add, add1,methodType){
alert("Message from apply method " +this.name + " is "+ this.age +" years old and he stays at "+ add + " ,Odisha , " + add1 +" and Method type is "+ methodType);
}
var arr=["Balasore", "India","Apply"];
sayApply.apply(obj, arr);
//Bind Example
function sayBind(add,methodType){
alert("Message from Bind method " + this.name + " is "+ this.age +" years old and he stays at "+ add + " ,Odisha and Method type is "+ methodType);
}
var objBind= sayBind.bind(obj);
objBind("Balsore","Bind")