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
To Invoke the name inside learn not newStudent
learn.study.call(learn, "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
Passing as this
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
this.learn
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
this.learn --> let is changed to var
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