Skip to content

Add documentation for the startIndex argument of restArguments #3003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1818,6 +1818,28 @@ <h2 id="functions">Function (uh, ahem) Functions</h2>
_.each(everyoneElse, sendConsolations);
});

raceResults("Dopey", "Grumpy", "Happy", "Sneezy", "Bashful", "Sleepy", "Doc");
</pre>
<p id="restArguments-startIndex">
The <b>startIndex</b> is mainly useful if the number of arguments cannot be reliably determined from the <b>function</b> itself, for example because <b>function</b> was output by another function.
</p>
<pre>
// The inner function from before.
function consoleNonWinners(gold, silver, bronze, everyoneElse) {
_.each(everyoneElse, sendConsolations);
}

// This time, we transform it through another metafunction first.
// Snow White always wins gold!
var withSnowWhite = _.partial(consoleNonWinners, "Snow White");

// withSnowWhite.length is zero because _.partial does not remember the
// number of parameters for us. We fix this by passing an explicit
// startIndex.
var raceResults = _.restArguments(withSnowWhite, 2);

// Dopey degraded to silver, Grumpy degraded to bronze and Happy fell
// out of the prizes altogether. We will console him as well.
raceResults("Dopey", "Grumpy", "Happy", "Sneezy", "Bashful", "Sleepy", "Doc");
</pre>

Expand Down