Skip to content

Instantly share code, notes, and snippets.

@ositowang
Created April 3, 2019 17:57
Show Gist options
  • Save ositowang/0f5ae61c30b41b5559804e1bf04f7f17 to your computer and use it in GitHub Desktop.
Save ositowang/0f5ae61c30b41b5559804e1bf04f7f17 to your computer and use it in GitHub Desktop.
A simple version of bind() in js.
/**
* 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