FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [View Raw Code]   [Original HTTPS Page]

til_example/javascript/splat-arguments-to-a-function.md at master · aaajit/til_example · GitHub

forked from jbranchaud/til

Latest commit

 

History

History
19 lines (15 loc) · 478 Bytes

File metadata and controls

19 lines (15 loc) · 478 Bytes

Splat Arguments To A Function

Often times you have a function that takes a certain set of arguments. Like the following adder function:

var adder = function(a,b,c) {
  return a + b + c;
};

But you are left trying to pass in arguments as an array (e.g. [1,2,3]). You want to be able to splat the array of arguments so that it matches the function declaration. This can be done by using apply.

> adder.apply(undefined, [1,2,3])
6

Back | FazBrowse Home | New Git URL