Created
April 3, 2019 17:57
-
-
Save ositowang/0f5ae61c30b41b5559804e1bf04f7f17 to your computer and use it in GitHub Desktop.
A simple version of bind() in js.
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
/** | |
* Concat the parameters passed in when binding and the parameters when the new | |
* binded function takes | |
* | |
* @param {*} context | |
* @returns | |
*/ | |
Function.prototype.bindWithParam = function(context) { | |
// take the function | |
let fn = this; | |
let args = [...arguments].slice(1); | |
return function() { | |
//this is the arguments passed in when the binded function invoked | |
let bindFuncArgs = [...arguments]; | |
fn.apply(context, args.concat(bindFuncArgs)); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment