Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sergiopichardo/e1300cb435e6925c695f2a0db31eae6e to your computer and use it in GitHub Desktop.
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
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