0 points

call and bind


dashboard

Sat Oct 24 2020 07:32:55 GMT+0000 (UTC)

Posted by @snigdhakhurana10

const person = { name: 'Lydia' };

function sayHi(age) {
  return `${this.name} is ${age}`;
}

console.log(sayHi.call(person, 21));
console.log(sayHi.bind(person, 21));

//With both, we can pass the object to which we want the this keyword to refer to. However, .call is executed immediately!

//.bind returns a copy of the function, but with a bound context! It is not executed immediately.
content_copy Copy