Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save TravelingMan/da760d15a6ad9362a0fa to your computer and use it in GitHub Desktop.
Save TravelingMan/da760d15a6ad9362a0fa to your computer and use it in GitHub Desktop.
From MDN. Using variable.apply() to allow the use of an array in place of standard delimited arguments without rewriting the original function.
// From MDN
function avg() {
var sum = 0;
for (var i = 0, j = arguments.length; i < j; i++) {
sum += arguments[i];
}
return sum / arguments.length;
}
avg(2, 3, 4, 5); // 3.5
// Use the above function with an array without rewritting it
avg.apply(null, [2, 3, 4, 5]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment