Created
April 20, 2022 05:06
-
-
Save anibal21/aee0c194c9d706ff1fea3d25fd67dd94 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* 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