Skip to content

Commit 80392bd

Browse files
committed
Improve examples
1 parent 881adc0 commit 80392bd

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

JavaScript/2-middle.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
'use strict';
22

3-
const adder = initial => ({
3+
const adder = (initial = 0) => ({
44
value: initial,
5+
steps: [initial],
56
add(value) {
7+
this.steps.push(value);
68
this.value += value;
79
return this;
810
}
911
});
1012

1113
const Adder = class {
12-
constructor(initial) {
14+
constructor(initial = 0) {
15+
this.steps = [initial];
1316
this.value = initial;
1417
}
1518
add(value) {
19+
this.steps.push(value);
1620
this.value += value;
1721
return this;
1822
}
@@ -21,11 +25,15 @@ const Adder = class {
2125
// Usage
2226

2327
{
24-
const v = new Adder(3).add(-9).add(12);
25-
console.log(v);
28+
const { value, steps } = new Adder(5).add(-8).add(11);
29+
console.log(value);
30+
const [a, b, c] = steps;
31+
console.log(a, b, c);
2632
}
2733

2834
{
29-
const v = adder(3).add(-9).add(12);
30-
console.log(v);
35+
const { value, steps } = adder(5).add(-8).add(11);
36+
console.log(value);
37+
const [a, b, c] = steps;
38+
console.log(a, b, c);
3139
}

JavaScript/3-extended.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ const PointEx = serializable(movable(Point));
2626
const point1 = new PointEx(10, 20);
2727
point1.move(5, -2);
2828
console.log(point1.toString());
29+
const { x, y } = point1;
30+
console.log(x, y);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy