use call(), apply(), & bind() method in same object - 08

Use the call() method in the same object without arguments:

const person = {
      name: "Robert",
      age: 23,
      color: "black",
      student: {
        name: "Elton",
        age: 25,
        purchase: function (city, country) {
          console.log(`${this.name} is a programmer, his age is ${this.age}.`);
        },
      },
};

person.student.purchase.call(person);
// Result: Robert is a programmer, his age is 23.


Use the call() method in the same object with arguments:

const person = {
      name: "Robert",
      age: 23,
      color: "black",
      student: {
        name: "Elton",
        age: 25,
        purchase: function (city, country) {
          console.log(
            `${this.name} is a programmer, his age is ${this.age}. He lives in ${city}, ${country}.`,
          );
        },
      },
};

person.student.purchase.call(person, "Dhaka", "Bangladesh");
// Result: Robert is a programmer, his age is 23. He lives in Dhaka, Bangladesh.


Use apply() method in the same object without arguments:

const person = {
      name: "Robert",
      age: 23,
      color: "black",
      student: {
        name: "Elton",
        age: 25,
        purchase: function () {
          console.log(`${this.name} is a programmer, his age is ${this.age}.`);
        },
      },
    };

    person.student.purchase.apply(person);
    // Result: Robert is a programmer, his age is 23.


Use apply() method in the same object with arguments:

const person = {
      name: "Robert",
      age: 23,
      color: "black",
      student: {
        name: "Elton",
        age: 25,
        purchase: function (city, country) {
          console.log(
            `${this.name} is a programmer, his age is ${this.age}. He lives in ${city}, ${country}.`,
          );
        },
    },
};

person.student.purchase.apply(person, ["Dhaka", "Bangladesh"]);
// Result: Robert is a programmer, his age is 23. He lives in Dhaka, Bangladesh.

Use the bind() method in the same object without arguments:

const person = {
      name: "Robert",
      age: 23,
      color: "black",
      student: {
        name: "Elton",
        age: 25,
        purchase: function () {
          console.log(`${this.name} is a programmer, his age is ${this.age}.`);
        },
      },
};

const bindResult = person.student.purchase.bind(person);
bindResult();
// Result: Robert is a programmer, his age is 23.


Use the bind() method in the same object with arguments:

 const person = {
      name: "Robert",
      age: 23,
      color: "black",
      student: {
        name: "Elton",
        age: 25,
        purchase: function (city, country) {
          console.log(
            `${this.name} is a programmer, his age is ${this.age}. He lives in ${city}, ${country}`,
          );
        },
      },
};

const bindResult = person.student.purchase.bind(person, "Dhaka", "Bangladesh");
bindResult();
// Result: Robert is a programmer, his age is 23. He lives in Dhaka, Bangladesh.


Post a Comment

Previous Post Next Post