loading...

Permanently assign `this` to a function

318097 profile image Mehul Lakhanpal Originally published at codedrops.tech ・1 min read
const obj = { name: "Codedrops" };

function greetings() {
  console.log(`Hi, ${this.name}`);
}

const bindGreetingToObj = greetings.bind(obj);
bindGreetingToObj(); // Hi, Codedrops

const obj2 = { name: "ABC" };
bindGreetingToObj.call(obj2); // Hi, Codedrops
Enter fullscreen mode Exit fullscreen mode

Thanks for reading 💙

Follow @codedrops.tech for daily posts.

InstagramTwitterFacebook

Micro-Learning ● Web Development ● Javascript ● MERN stack ● Javascript

codedrops.tech

Discussion

pic
Editor guide