call , apply and bind

call

				
					let learn = {
  name: "Anurag",
  study: function (course) {
    console.log(this.name + " is learning " + course);
  },
};

let newStudent = {
  name: "Abhishek",
};

learn.study.call(newStudent, "Javascript"); //Abhishek is learning Javascript
				
			
				
					let learn = {
  name: "Anurag",
  study: function (course) {
    console.log(this.name + " is learning " + course);
  },
};

let newStudent = {
  name: "Abhishek",
};

learn.study.call(learn, "Javascript"); //Anurag is learning Javascript
				
			
				
					let learn = {
  name: "Anurag",
  study: function (course) {
    console.log(this.name + " is learning " + course);
  },
};

let newStudent = {
  name: "Abhishek",
};

learn.study.call(this, "Javascript"); // is learning Javascript
				
			
				
					let learn = {
  name: "Anurag",
  study: function (course) {
    console.log(this.name + " is learning " + course);
  },
};

let newStudent = {
  name: "Abhishek",
};

learn.study.call(this.learn, "Javascript"); // is learning Javascript
				
			
				
					var learn = {
  name: "Anurag",
  study: function (course) {
    console.log(this.name + " is learning " + course);
  },
};

let newStudent = {
  name: "Abhishek",
};

learn.study.call(this.learn, "Javascript"); //Anurag is learning Javascript
				
			

apply

				
					let learn = {
  name: "Anurag",
  study: function (course) {
    console.log(this.name + " is learning " + course);
  },
};

let newStudent = {
  name: "Abhishek",
};

//apply takes array in the second parameter
learn.study.apply(newStudent, ["Javascript"]); //Abhishek is learning Javascript

				
			

bind

				
					let learn = {
  name: "Anurag",
  study: function (course) {
    console.log(this.name + " is learning " + course);
  },
};

let newStudent = {
  name: "Abhishek",
};

//bind as below ..reference has to be stored..invoke it after that
const learnRef = learn.study.bind(newStudent);
learnRef("Advance Javascript"); //Abhishek is learning Advance Javascript
				
			

Leave a Comment