Skip to content

Instantly share code, notes, and snippets.

@anibal21
Created April 20, 2022 05:06
Show Gist options
  • Save anibal21/aee0c194c9d706ff1fea3d25fd67dd94 to your computer and use it in GitHub Desktop.
Save anibal21/aee0c194c9d706ff1fea3d25fd67dd94 to your computer and use it in GitHub Desktop.
/**
* Explanation with an interview example from real life
* Call invokes the function and allows you to pass in arguments one by one.
* Apply invokes the function and allows you to pass in arguments as an array.
* Bind returns a new function, allowing you to pass in a this array and any number of arguments.
**/
// Call - Example
const callSomeone = (name, callback) => {
const innerMessage= {
message: name + ' Hi Mothafucka'
}
callback.call(innerMessage)
}
function telephone (name) {
return callSomeone(name, function () {
console.log(this.message)
})
}
telephone('Anibal')
// Apply - Example
const callSomeone = (name, callback) => {
const innerMessage= {
message: name + ' Hi Mothafucka'
}
callback.apply(innerMessage)
}
function telephone (name) {
return callSomeone(name, function () {
console.log(this.message)
})
}
telephone('Anibal')
// Bind - example
const callSomeone = (name, callback) => {
const innerMessage= {
message: name + ' Hi Mothafucka'
}
callback.bind(innerMessage)()
}
function telephone (name) {
return callSomeone(name, function () {
console.log(this.message)
})
}
telephone('Anibal')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment