Skip to content

Instantly share code, notes, and snippets.

@apcurran
Created June 4, 2021 20:09
Show Gist options
  • Select an option

  • Save apcurran/def32283cadc6eff3f08faf4f699eec9 to your computer and use it in GitHub Desktop.

Select an option

Save apcurran/def32283cadc6eff3f08faf4f699eec9 to your computer and use it in GitHub Desktop.
Use of bind and call with JS functions
"use strict";
// func.bind();
const person = {
name: "Alex",
sayHi() {
return `Hey ${this.name}`;
}
};
const sayHi = person.sayHi.bind(person);
console.log(sayHi());
// func.call();
function sayHello() {
console.log(this.name);
}
const user = { name: "Sam" };
const admin = { name: "Beth" };
sayHello.call(user); // Sam
sayHello.call(admin); // Beth
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment