File tree 3 files changed +60
-1
lines changed
examples/chapter01/typescript
3 files changed +60
-1
lines changed Original file line number Diff line number Diff line change 20
20
"rules" : {
21
21
"class-methods-use-this" : 0 ,
22
22
"no-plusplus" : 0 ,
23
- "arrow-parens" : 0
23
+ "arrow-parens" : 0 ,
24
+ "no-console" : 0
24
25
}
25
26
}
Original file line number Diff line number Diff line change @@ -6,3 +6,25 @@ var language = 'JavaScript'; // string
6
6
var favoriteLanguage ;
7
7
var langs = [ 'JavaScript' , 'Ruby' , 'Python' ] ;
8
8
favoriteLanguage = langs [ 0 ] ;
9
+ function printName ( person ) {
10
+ console . log ( person . name ) ;
11
+ }
12
+ var john = { name : 'John' , age : 21 } ;
13
+ var mary = { name : 'Mary' , age : 21 , phone : '123-45678' } ;
14
+ printName ( john ) ;
15
+ printName ( mary ) ;
16
+ var MyObject = /** @class */ ( function ( ) {
17
+ function MyObject ( ) {
18
+ }
19
+ MyObject . prototype . compareTo = function ( b ) {
20
+ if ( this . age === b . age ) {
21
+ return 0 ;
22
+ }
23
+ return this . age > b . age ? 1 : - 1 ;
24
+ } ;
25
+ return MyObject ;
26
+ } ( ) ) ;
27
+ function compareTwoObjects ( a , b ) {
28
+ console . log ( a . compareTo ( b ) ) ;
29
+ console . log ( b . compareTo ( a ) ) ;
30
+ }
Original file line number Diff line number Diff line change @@ -8,3 +8,39 @@ let language = 'JavaScript'; // string
8
8
let favoriteLanguage : string ;
9
9
let langs = [ 'JavaScript' , 'Ruby' , 'Python' ] ;
10
10
favoriteLanguage = langs [ 0 ] ;
11
+
12
+ interface Person {
13
+ name : string ;
14
+ age : number ;
15
+ }
16
+
17
+ function printName ( person : Person ) {
18
+ console . log ( person . name ) ;
19
+ }
20
+
21
+ const john = { name : 'John' , age : 21 } ;
22
+ const mary = { name : 'Mary' , age : 21 , phone : '123-45678' } ;
23
+ printName ( john ) ;
24
+ printName ( mary ) ;
25
+
26
+ interface Comparable < T > {
27
+ compareTo ( b : T ) : number ;
28
+ }
29
+
30
+ class MyObject implements Comparable < MyObject > {
31
+ age : number ;
32
+
33
+ compareTo ( b : MyObject ) : number {
34
+ if ( this . age === b . age ) {
35
+ return 0 ;
36
+ }
37
+ return this . age > b . age ? 1 : - 1 ;
38
+ }
39
+ }
40
+
41
+ function compareTwoObjects ( a : MyObject , b : MyObject ) {
42
+ console . log ( a . compareTo ( b ) ) ;
43
+ console . log ( b . compareTo ( a ) ) ;
44
+ }
45
+
46
+
You can’t perform that action at this time.
0 commit comments