30秒学会 JavaScript 片段 · 2023年3月10日

30秒学会 JavaScript 片段 – Prepend function arguments

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!'

翻译自:https://www.30secondsofcode.org/js/s/prepend-function-arguments