O'Reilly logo

Mastering JavaScript Functional Programming by Federico Kereki

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Partial application with eval()

If we want to be able to do partial application fixing any combination of parameters, we must have a way to specify which arguments are to be left free and which will be fixed from that point on. Some libraries, such as Underscore or LoDash, use a special object, _, to signify an omitted parameter. In this fashion, still using the same nonsense() function, we would write the following:

const fix2and5 = _.partial(nonsense, _, 22, _, _, 1960);

We could do the same sort of thing, by having a global variable that would represent a pending, not yet fixed argument, but let's make it simpler, and just write undefined to represent a missing parameter.

When checking for undefined, remember to always use the === operator; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required