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