30秒学会 JavaScript 片段 · 2020年3月14日

30秒学会 JavaScript 片段 – partialRight

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

Use the spread operator (...) to append partials to the list of arguments of fn.

代码片段

const partialRight = (fn, ...partials) => (...args) => fn(...args, ...partials);

使用样例

const greet = (greeting, name) => greeting + ' ' + name + '!';
const greetJohn = partialRight(greet, 'John');
greetJohn('Hello'); // 'Hello John!'