function
that uses Function.prototype.apply()
to bind context[fn]
to context
....
) to prepend any additional supplied parameters to the arguments.const bindKey = (context, fn, ...boundArgs) => (...args) => context[fn].apply(context, [...boundArgs, ...args]);
const freddy = { user: 'fred', greet: function(greeting, punctuation) { return greeting + ' ' + this.user + punctuation; } }; const freddyBound = bindKey(freddy, 'greet'); console.log(freddyBound('hi', '!')); // 'hi fred!'
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️