File tree Expand file tree Collapse file tree 3 files changed +28
-16
lines changed
Expand file tree Collapse file tree 3 files changed +28
-16
lines changed Original file line number Diff line number Diff line change @@ -18,8 +18,8 @@ class CreditAccount extends Account {
1818 }
1919}
2020
21- const accounts : Account [ ] = [ new SavingsAccount ( 5000 ) , new CreditAccount ( - 750 ) ] ;
21+ const a1 = new SavingsAccount ( 5000 ) ;
22+ console . log ( a1 . describe ( ) ) ;
2223
23- for ( const account of accounts ) {
24- console . log ( account . describe ( ) ) ;
25- }
24+ const a2 = new CreditAccount ( - 750 ) ;
25+ console . log ( a2 . describe ( ) ) ;
Original file line number Diff line number Diff line change 22
33// Subtype polymorphism: protocol/duck typing
44
5- const describe = ( entity ) => {
6- if ( typeof entity . describe !== 'function' ) {
7- throw new TypeError ( 'Requires describe method' ) ;
8- }
9- entity . describe ( ) ;
10- } ;
11-
125class SavingsAccount {
136 constructor ( balance ) {
147 this . balance = balance ;
158 }
169
1710 describe ( ) {
18- console . log ( `Savings -> Balance: ${ this . balance } ` ) ;
11+ return `Savings -> Balance: ${ this . balance } ` ;
1912 }
2013}
2114
@@ -25,9 +18,24 @@ class CreditAccount {
2518 }
2619
2720 describe ( ) {
28- console . log ( `Credit -> Balance: ${ this . balance } ` ) ;
21+ return `Credit -> Balance: ${ this . balance } ` ;
2922 }
3023}
3124
32- describe ( new SavingsAccount ( 5000 ) ) ;
33- describe ( new CreditAccount ( - 750 ) ) ;
25+ const getInfo = ( entity ) => {
26+ if ( typeof entity . describe !== 'function' ) {
27+ throw new TypeError ( 'Requires describe method' ) ;
28+ }
29+ const res = entity . describe ( ) ;
30+ console . log ( res ) ;
31+ } ;
32+
33+ const account = {
34+ describe ( ) {
35+ return 'Stub account' ;
36+ } ,
37+ } ;
38+
39+ getInfo ( account ) ;
40+ getInfo ( new SavingsAccount ( 5000 ) ) ;
41+ getInfo ( new CreditAccount ( - 750 ) ) ;
Original file line number Diff line number Diff line change @@ -24,7 +24,11 @@ class CreditAccount extends Account {
2424 }
2525}
2626
27- const accounts = [ new SavingsAccount ( 5000 ) , new CreditAccount ( - 750 ) ] ;
27+ const accounts = [
28+ new SavingsAccount ( 5000 ) ,
29+ new CreditAccount ( - 750 ) ,
30+ { describe : ( ) => 'Account stub' } ,
31+ ] ;
2832
2933for ( const account of accounts ) {
3034 console . log ( account . describe ( ) ) ;
You can’t perform that action at this time.
0 commit comments