File tree 2 files changed +6
-4
lines changed
1-js/04-object-basics/04-object-methods/8-chain-calls
2 files changed +6
-4
lines changed Original file line number Diff line number Diff line change @@ -23,7 +23,7 @@ let ladder = {
23
23
}
24
24
};
25
25
26
- ladder .up ().up ().down ().up ().down ().showStep (); // 1
26
+ ladder .up ().up ().down ().showStep ().down ().showStep (); // shows 1 then 0
27
27
```
28
28
29
29
We also can write a single call per line. For long chains it's more readable:
33
33
.up ()
34
34
.up ()
35
35
.down ()
36
- .up ()
36
+ .showStep () // 1
37
37
.down ()
38
- .showStep (); // 1
38
+ .showStep (); // 0
39
39
```
Original file line number Diff line number Diff line change @@ -28,12 +28,14 @@ ladder.up();
28
28
ladder .up ();
29
29
ladder .down ();
30
30
ladder .showStep (); // 1
31
+ ladder .down ();
32
+ ladder .showStep (); // 0
31
33
```
32
34
33
35
Modify the code of ` up ` , ` down ` and ` showStep ` to make the calls chainable, like this:
34
36
35
37
``` js
36
- ladder .up ().up ().down ().showStep (); // 1
38
+ ladder .up ().up ().down ().showStep (). down (). showStep () ; // shows 1 then 0
37
39
```
38
40
39
41
Such approach is widely used across JavaScript libraries.
You can’t perform that action at this time.
0 commit comments