From b951071f1275c5598381aeb31724354ff27645c9 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Sat, 5 Dec 2015 14:37:22 -0600 Subject: [PATCH] Add Splat Arguments To A Function as a javascript til. --- README.md | 1 + javascript/splat-arguments-to-a-function.md | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 javascript/splat-arguments-to-a-function.md diff --git a/README.md b/README.md index 9f4f78c..ac5f1d9 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,7 @@ smart people at [Hashrocket](http://hashrocket.com/). ### javascript - [Character Codes from Keyboard Listeners](javascript/character-codes-from-keyboard-listeners.md) +- [Splat Arguments To A Function](javascript/splat-arguments-to-a-function.md) - [Throttling A Function Call](javascript/throttling-a-function-call.md) - [Truthiness of Integer Arrays](javascript/truthiness-of-integer-arrays.md) diff --git a/javascript/splat-arguments-to-a-function.md b/javascript/splat-arguments-to-a-function.md new file mode 100644 index 0000000..501b2ff --- /dev/null +++ b/javascript/splat-arguments-to-a-function.md @@ -0,0 +1,19 @@ +# Splat Arguments To A Function + +Often times you have a function that takes a certain set of arguments. Like +the following `adder` function: + +```javascript +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`. + +```javascript +> adder.apply(undefined, [1,2,3]) +6 +```