Introduction
In this lab, we will explore the concept of function partials in JavaScript and learn how to create a higher-order function that allows us to easily prepend arguments to a given function. Through practical examples and exercises, we will understand how partial functions work and how they can be used to simplify our code and make it more modular.
Function Arguments Prepended with Partial
To begin practicing coding, open the Terminal/SSH and enter node.
The function partial is used to create a new function that calls fn with partials as the first arguments.
- Use the spread operator (
...) to prependpartialsto the list of arguments offn.
const partial =
(fn, ...partials) =>
(...args) =>
fn(...partials, ...args);
const greet = (greeting, name) => greeting + " " + name + "!";
const greetHello = partial(greet, "Hello");
greetHello("John"); // 'Hello John!'
Summary
Congratulations! You have completed the Prepend Function Arguments lab. You can practice more labs in LabEx to improve your skills.