Skip to content

Commit 7c6b55c

Browse files
committed
spread operator
1 parent 231232c commit 7c6b55c

File tree

5 files changed

+59
-0
lines changed

5 files changed

+59
-0
lines changed

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
* [Destructuring](docs/destructuring.md)
1111
* [for...of](docs/for...of.md)
1212
* [Template Strings](docs/template-strings.md)
13+
* [Spread Operator](docs/spread-operator.md)

code/es6/spread-operator.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var spread;
2+
(function (spread) {
3+
var list = [
4+
1,
5+
2
6+
];
7+
list = list.concat([3, 4]);
8+
})(spread = exports.spread || (exports.spread = {}));

code/es6/spread-operator.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export module spread {
2+
var list = [1, 2];
3+
list = [...list, 3, 4];
4+
}

code/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"./es6/for..of.ts",
1919
"./es6/let.ts",
2020
"./es6/rest-parameters.ts",
21+
"./es6/spread-operator.ts",
2122
"./es6/template-strings.ts",
2223
"./es6/test.ts",
2324
"./testing.ts"

docs/spread-operator.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
### Spread Operator
2+
3+
The main objective of the spread operator is to *spread* the objects of an array. This is best explained with examples.
4+
5+
#### Apply
6+
A common use case it to spread an array into the function arguments. Previously you would need to use `Function.prototype.apply`:
7+
8+
```ts
9+
function foo(x, y, z) { }
10+
var args = [0, 1, 2];
11+
foo.apply(null, args);
12+
```
13+
14+
Now you can do this simply by prefixing the arguments with `...` as shown below:
15+
16+
```ts
17+
function foo(x, y, z) { }
18+
var args = [0, 1, 2];
19+
foo(...args);
20+
```
21+
22+
Here we are *spreading* the `args` array into positional `arguments`.
23+
24+
#### Destructuring
25+
We've already seen one usage of this in *destructuring*
26+
27+
```ts
28+
var [x, y, ...remaining] = [1, 2, 3, 4];
29+
console.log(x, y, remaining); // 1, 2, [3,4]
30+
```
31+
The motivation here is to simply make it easy for you to capture the remaining elements of an array when destructuring.
32+
33+
#### Array Assignment
34+
The spread operator allows you easily place an *expanded version* of an array into another array. This is demonstrated in the below example:
35+
36+
```ts
37+
var list = [1, 2];
38+
list = [...list, 3, 4];
39+
console.log(list); // [1,2,3,4]
40+
```
41+
42+
#### Summary
43+
`apply` is something that you would inevitably do in JavaScript, so its good to have a better syntax where you don't have that ugly `null` for the `this` argument. Also having a dedicated syntax for moving arrays out of (destructuring) or into (assignment) other arrays provides neat syntax for when you are doing array processing on partial arrays.
44+
45+
{% include "footer.md" %}

0 commit comments

Comments
 (0)