Skip to content

Commit 73cc05d

Browse files
author
Basarat Ali Syed
committed
add object spread and object rest docs 🌹
1 parent 0020ce4 commit 73cc05d

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
* [let](docs/let.md)
1616
* [const](docs/const.md)
1717
* [Destructuring](docs/destructuring.md)
18+
* [Spread Operator](docs/spread-operator.md)
1819
* [for...of](docs/for...of.md)
1920
* [Iterators](docs/iterators.md)
2021
* [Template Strings](docs/template-strings.md)
21-
* [Spread Operator](docs/spread-operator.md)
2222
* [Promise](docs/promise.md)
2323
* [Generators](docs/generators.md)
2424
* [Async Await](docs/async-await.md)

docs/destructuring.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,28 @@ var foo = { bar: { bas: 123 } };
5050
var {bar: {bas}} = foo; // Effectively `var bas = foo.bar.bas;`
5151
```
5252

53+
#### Object Destructuring with rest
54+
You can pick up any number of elements from the an object and get *an object* of the remaining elements using object destructuring with rest.
55+
56+
```ts
57+
var {w, x, ...remaining} = {w: 1, x: 2, y: 3, z: 4};
58+
console.log(w, x, remaining); // 1, 2, {y:3,z:4}
59+
```
60+
A common use case is also to ignore certain properties. For example:
61+
```ts
62+
// Example function
63+
function goto(point2D: {x: number, y: number}) {
64+
// Imagine some code that might break
65+
// if you pass in an object
66+
// with more items than desired
67+
}
68+
// Some point you get from somewhere
69+
const point3D = {x: 1, y: 2, z: 3};
70+
/** A nifty use of rest to remove extra properties */
71+
const { z, ...point2D } = point3D;
72+
goto(point2D);
73+
```
74+
5375
#### Array Destructuring
5476
A common programming question: "How to swap two variables without using a third one?". The TypeScript solution:
5577

docs/spread-operator.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
### Spread Operator
22

3-
The main objective of the spread operator is to *spread* the objects of an array. This is best explained with examples.
3+
The main objective of the spread operator is to *spread* the elements of an array or object. This is best explained with examples.
44

55
#### Apply
66
A common use case is to spread an array into the function arguments. Previously you would need to use `Function.prototype.apply`:
@@ -39,6 +39,24 @@ list = [...list, 3, 4];
3939
console.log(list); // [1,2,3,4]
4040
```
4141

42+
#### Object spread
43+
You can also spread an object into another object. A common use case is to simply add a property to an object without mutating the original:
44+
45+
```ts
46+
const point2d = {x: 1, y: 2};
47+
/** Create a new object by using all the point2D props along with z */
48+
const point3D = {...point2D, z: 3};
49+
```
50+
51+
Another common use case is a simple shallow extend:
52+
53+
```ts
54+
const foo = {a: 1, b: 2};
55+
const bar = {c: 1, d: 2};
56+
/** Merge foo and bar */
57+
const fooBar = {...foo, ...bar};
58+
```
59+
4260
#### Summary
4361
`apply` is something that you would inevitably do in JavaScript, so it's 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.
4462

0 commit comments

Comments
 (0)