Created
July 30, 2021 01:53
-
-
Save sergiopichardo/e1300cb435e6925c695f2a0db31eae6e to your computer and use it in GitHub Desktop.
A function can lose its surrounding context when a function is passed as an argument to another function
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
| function fetchData() { | |
| let sergiosTasks = { | |
| tasks: [ | |
| 'study oop javascript', | |
| 'prepare for sysops exam', | |
| 'read linux book', | |
| 'write article' | |
| ], | |
| getTasks() { | |
| console.log('Today I have to:') | |
| this.tasks.forEach((task, index) => { | |
| console.log(`(${index + 1}) ${task}`); | |
| }) | |
| } | |
| } | |
| logTasks(sergiosTasks.getTasks); // context is lost | |
| } | |
| function logTasks(callback) { | |
| callback(); | |
| } | |
| fetchData(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment