Created
May 10, 2015 20:16
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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