You are on page 1of 2

var obj1 = {x:10};

var doThis = function(a){


return this.x + a;
}

// How to call "doThis" on obj1

doThis.call(obj1, 5); // 15

-------------------------------------------------
apply

var obj1 = {x:10};


var doThis = function(a,b,c){
return this.x + a + b + c;
}
var arr = [3,4,5]
doThis.apply(obj1, arr); // 22

----------------------------------------------------------------

Bind :-

It only creates a copy of the function and returns it.

var obj1 = {x:10};


var doThis = function(a,b,c){
return this.x + a + b + c;
}
aa = doThis.bind(obj1);
aa(1,2,3);

---------------------------------

var greet = function(name){


console.log(this.toUpperCase() + " " + name);
}

//greet.call("cool","sam") //COOL sam


//greet.apply("cool",["sam"]) //COOL sam

-----------------------------------
var obj1 = {
name: "sam",
doSomething: function(){
return this.name;
}
}

foo = {name: "ash"}


bar = {name: "pari"}
obj1.doSomething.call(foo)

//////Return Object

function getMenu(){
return this;
}
hrMenu = {menu:"pizza"};

aa = getMenu.bind(hrMenu);
aa() // {menu:pizza}

You might also like