We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f0d43fb commit dc28bf5Copy full SHA for dc28bf5
JavaScript/7-chaining.js
@@ -0,0 +1,39 @@
1
+'use strict';
2
+
3
+// Imperative
4
5
+class Adder {
6
+ constructor(initial) {
7
+ this.value = initial;
8
+ }
9
+ add(value) {
10
+ this.value += value;
11
+ return this;
12
13
+ valueOf() {
14
+ return this.value;
15
16
+}
17
18
+const sum1 = new Adder(1).add(9).add(1).add(7);
19
+console.log('Sum:', +sum1);
20
21
+// Functional
22
23
+const adder = initial => Object.assign(
24
+ value => adder(initial + value),
25
+ { valueOf: () => initial }
26
+);
27
28
+const sum2 = adder(1)(9)(1)(7);
29
+console.log('Sum:', +sum2);
30
31
32
33
+const add = initial => ({
34
+ add: value => add(initial + value),
35
+ valueOf: () => initial
36
+});
37
38
+const sum3 = add(1).add(9).add(1).add(7);
39
+console.log('Sum:', +sum3);
0 commit comments