Prepend function arguments

OSCC · JavaScript, Function · Sep 15, 2020

Creates a function that invokes fn with partials prepended to the arguments it receives.

  • Use the spread operator (...) to prepend partials to the list of arguments of fn.
const partial = (fn, ...partials) => (...args) => fn(...partials, ...args);
const greet = (greeting, name) => greeting + ' ' + name + '!';
const greetHello = partial(greet, 'Hello');
greetHello('John'); // 'Hello John!'

Recommended snippets

  • Append function arguments

    JavaScript, Function

    Creates a function that invokes fn with partials appended to the arguments it receives.

  • Bind function context

    JavaScript, Function

    Creates a function that invokes fn with a given context, optionally prepending any additional supplied parameters to the arguments.

  • Bind object method

    JavaScript, Function

    Creates a function that invokes the method at a given key of an object, optionally prepending any additional supplied parameters to the arguments.