Created
June 4, 2021 20:09
-
-
Save apcurran/def32283cadc6eff3f08faf4f699eec9 to your computer and use it in GitHub Desktop.
Use of bind and call with JS functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "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