File tree Expand file tree Collapse file tree 9 files changed +259
-1
lines changed
Expand file tree Collapse file tree 9 files changed +259
-1
lines changed Original file line number Diff line number Diff line change 4949 - [ Section 6.2: Function as a parameter] ( /book_pages/chapter6/section6.2.md )
5050 - [ Section 6.3: Functions with Union Types] ( /book_pages/chapter6/section6.3.md )
5151 - [ Section 6.4: Types of Functions] ( /book_pages/chapter6/section6.4.md )
52+
53+ * Chapter 7: Classes
54+
55+ - [ Section 7.1: Abstract Classes] ( /book_pages/chapter7/section7.1.md )
56+ - [ Section 7.2: Simple class] ( /book_pages/chapter7/section7.2.md )
57+ - [ Section 7.3: Basic Inheritance] ( /book_pages/chapter7/section7.3.md )
58+ - [ Section 7.4: Constructors] ( /book_pages/chapter7/section7.4.md )
59+ - [ Section 7.5: Accessors] ( /book_pages/chapter7/section7.5.md )
60+ - [ Section 7.6: Transpilation] ( /book_pages/chapter7/section7.6.md )
61+ - [ Section 7.7: Monkey patch a function into an existing class] ( /book_pages/chapter7/section7.7.md )
5262
Original file line number Diff line number Diff line change 1+ # Section 7.1: Abstract Classes
2+
3+ ``` ts
4+ abstract class Machine {
5+ constructor (public manufacturer : string ) {
6+
7+ }
8+
9+ // An abstract class can define methods of its own, or...
10+ summary(): string {
11+ return ` ${this .manufacturer } makes this machine. ` ;
12+ }
13+
14+ // Require inheriting classes to implement methods
15+ abstract moreInfo(): string ;
16+ }
17+
18+ class Car extends Machine {
19+ constructor (manufacturer : string , public position : number , protected speed : number ) {
20+ super (manufacturer );
21+ }
22+
23+ move() {
24+ this .position += this .speed ;
25+ }
26+
27+ moreInfo() {
28+ return ` This is a car located at ${this .position } and going ${this .speed }mph! ` ;
29+ }
30+ }
31+
32+ let myCar = new Car (" Konda" , 30 , 70 );
33+ myCar .move (); // position is now 80
34+ console .log (myCar .summary ()); // prints "Konda makes this machine."
35+ console .log (myCar .moreInfo ()); // prints "This is a car located at 80 and going 70mph!"
36+ ```
Original file line number Diff line number Diff line change 1+ # Section 7.2: Simple class
2+
3+ ``` ts
4+ class Car {
5+ public position: number = 50 ;
6+ private speed: number = 42 ;
7+
8+ move() {
9+ this .position += this .speed ;
10+ }
11+ }
12+
13+ var car = new Car (); // create an instance of Car
14+ car .move (); // call a method
15+ console .log (car .position ); // access a public property
16+ ```
Original file line number Diff line number Diff line change 1+ # Section 7.3: Basic Inheritance
2+
3+ ``` ts
4+ class Car {
5+ public position: number = 25 ;
6+ protected speed: number = 42 ;
7+
8+ move() {
9+ this .position += this .speed ;
10+ console .log (this .position );
11+ }
12+ }
13+
14+ class SelfDrivingCar extends Car {
15+ move() {
16+ // super.move();
17+ console .log (' Hi Morol' );
18+ }
19+ }
20+
21+ let myCar = new Car ();
22+ myCar .move (); // 67
23+
24+ myCar = new SelfDrivingCar ();
25+ myCar .move (); // Hi Morol
26+ ```
Original file line number Diff line number Diff line change 1+ # Section 7.4: Constructors
2+
3+ ``` ts
4+ class Car {
5+ public position: number ;
6+ protected speed: number ;
7+ constructor (position : number , speed : number ) {
8+ this .position = position ;
9+ this .speed = speed ;
10+ }
11+ move() {
12+ this .position += this .speed ;
13+ return this .position ;
14+ }
15+ }
16+
17+ let myCar = new Car (30 , 56 );
18+ console .log (myCar .move ()); // 86
19+ ```
20+
21+ ``` ts
22+ class Car {
23+ constructor (public position : number , protected speed : number ) {}
24+ move() {
25+ this .position += this .speed ;
26+ }
27+ }
28+ ```
29+
30+ ``` ts
31+ var Car = (function () {
32+ function Car(position , speed ) {
33+ this .position = position ;
34+ this .speed = speed ;
35+ }
36+
37+ Car .prototype .move = function () {
38+ this .position += this .speed ;
39+ };
40+
41+ return Car ;
42+ }());
43+
44+ let myCar = new Car (23 , 32 );
45+ console .log (myCar ); // Car { position: 23, speed: 32 }
46+ ```
47+
48+ Constructors of derived classes have to call the base class constructor with ** super()** .
49+ ``` ts
50+ class SelfDrivingCar extends Car {
51+ constructor (startAutoPilot : boolean ) {
52+ super (0 , 42 );
53+ if (startAutoPilot ) {
54+ this .move ();
55+ }
56+ }
57+ }
58+
59+ let car = new SelfDrivingCar (true );
60+ console .log (car .position ); // access the public property position
61+ ```
Original file line number Diff line number Diff line change 1+ # Section 7.5: Accessors
2+
3+ ``` ts
4+ class Car {
5+ public position: number = 0 ;
6+ private _speed: number = 42 ;
7+ private _MAX_SPEED = 100
8+
9+ move() {
10+ this .position += this ._speed ;
11+ }
12+
13+ get speed(): number {
14+ return this ._speed ;
15+ }
16+
17+ set speed(value : number ) {
18+ this ._speed = Math .max (value , this ._MAX_SPEED );
19+ }
20+ }
21+
22+ let car = new Car ();
23+ console .log (car .speed ); // 42
24+ car .speed = 120 ;
25+ console .log (car .speed ); // 120
26+ ```
Original file line number Diff line number Diff line change 1+ # Section 7.6: Transpilation
2+
3+ ### TypeScript source
4+ ``` ts
5+ class SomeClass {
6+ public static SomeStaticValue: string = " hello" ;
7+ public someMemberValue: number = 15 ;
8+ private somePrivateValue: boolean = false ;
9+
10+ constructor () {
11+ SomeClass .SomeStaticValue = SomeClass .getGoodbye ();
12+ this .someMemberValue = this .getFortyTwo ();
13+ this .somePrivateValue = this .getTrue ();
14+ }
15+
16+ public static getGoodbye(): string {
17+ return " goodbye!" ;
18+ }
19+
20+ public getFortyTwo(): number {
21+ return 42 ;
22+ }
23+
24+ private getTrue(): boolean {
25+ return true ;
26+ }
27+ }
28+ ```
29+
30+ ### JavaScript source
31+
32+ ``` js
33+ var SomeClass = (function () {
34+ function SomeClass () {
35+ this .someMemberValue = 15 ;
36+ this .somePrivateValue = false ;
37+ SomeClass .SomeStaticValue = SomeClass .getGoodbye ();
38+ this .someMemberValue = this .getFortyTwo ();
39+ this .somePrivateValue = this .getTrue ();
40+ }
41+
42+ SomeClass .getGoodbye = function () {
43+ return " goodbye!" ;
44+ };
45+
46+ SomeClass .prototype .getFortyTwo = function () {
47+ return 42 ;
48+ };
49+
50+ SomeClass .prototype .getTrue = function () {
51+ return true ;
52+ };
53+
54+ return SomeClass;
55+ }());
56+
57+ SomeClass .SomeStaticValue = " hello" ;
58+ ```
Original file line number Diff line number Diff line change 1+ # Section 7.7: Monkey patch a function into an existing class
2+
3+ Sometimes it's useful to be able to extend a class with new functions. For example
4+ let's suppose that a string should be converted to a camel case string. So we need to
5+ tell TypeScript, that String contains a function called toCamelCase , which returns a
6+ string .
7+ ``` ts
8+ interface String {
9+ toCamelCase(): string ;
10+ }
11+ ```
12+
13+ ### Now we can patch this function into the String implementation.
14+ ``` ts
15+ String .prototype .toCamelCase = function () : string {
16+ return this .replace (/ [^ a-z ] / ig , ' ' )
17+ .replace (/ (?:^ \w | [A-Z ] | \b \w | \s + )/ g , (match : any , index : number ) => {
18+ return + match === 0 ? " " : match [index === 0 ? ' toLowerCase' : ' toUpperCase' ]();
19+ });
20+ }
21+ ```
22+ If this extension of String is loaded, it's usable like this:
23+ ``` ts
24+ " This is an example" .toCamelCase (); // => "thisIsAnExample"
25+ ```
Original file line number Diff line number Diff line change 1- console . log ( 'Welcome to TypeScript Professionals' ) ;
1+ console . log ( 'Welcome to TypeScript Professionals' ) ;
You can’t perform that action at this time.
0 commit comments