mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 23:28:02 +00:00
Add Splat Arguments To A Function as a javascript til.
This commit is contained in:
@@ -92,6 +92,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
|||||||
### javascript
|
### javascript
|
||||||
|
|
||||||
- [Character Codes from Keyboard Listeners](javascript/character-codes-from-keyboard-listeners.md)
|
- [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)
|
- [Throttling A Function Call](javascript/throttling-a-function-call.md)
|
||||||
- [Truthiness of Integer Arrays](javascript/truthiness-of-integer-arrays.md)
|
- [Truthiness of Integer Arrays](javascript/truthiness-of-integer-arrays.md)
|
||||||
|
|
||||||
|
|||||||
19
javascript/splat-arguments-to-a-function.md
Normal file
19
javascript/splat-arguments-to-a-function.md
Normal file
@@ -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
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user