Last active
July 5, 2018 12:21
-
-
Save Dobby89/520dacfd8da5f00974cbd6a1b1e22aa3 to your computer and use it in GitHub Desktop.
Apply an initial set of params to a method. Similar to bind() but without having to pass `this`
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
/** | |
* partiallyApply | |
* | |
* Apply an initial set of params to a method. | |
* Similar to bind() but without having to pass `this` | |
* | |
* @param {function} fn | |
* @param {*} initialArgs | |
*/ | |
function partiallyApply(fn, ...initialArgs) { | |
return function partiallyApplied(...laterArgs) { | |
return fn(...initialArgs, ...laterArgs); | |
}; | |
} | |
/** | |
* Usage example | |
*/ | |
function person(name, age, occupation) { | |
///... | |
} | |
const joe = partiallyApply(person, 'Joe'); | |
joe(30); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment