Skip to content

Commit d832a86

Browse files
committed
4. multiple constructors and overloaded methods, lambda example in typescript named narrow function.
1 parent da9d053 commit d832a86

File tree

10 files changed

+216
-9
lines changed

10 files changed

+216
-9
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,31 @@
2020

2121
- **1. hello world** (function helo world)
2222
**output** : ```Hello, World! ```
23+
2324
- **2. first class** (simple class)
2425
**output** : ```elephant object = [object Object] ```
26+
2527
- **3. bigger class** : (getters, setters, constructor with params, access keywords, )
2628
**output** : ```elephant => name = elephant 2, age = 10, vaccination = true ```
2729

30+
- **4. more constructors and more methods** : trick how you can have overloaded methods (technicaly still one method)
31+
and multiple constructors (technically still one constructor) and lambda example.
32+
**output** : ```tiger1 => name = Tiger1, age = undefined, vaccination = undefined
33+
undefined
34+
24
35+
undefined
36+
24
37+
tiger2 => name = Tiger2, age = 14, vaccination = undefined
38+
14
39+
13
40+
14
41+
13
42+
tiger3 => name = Tiger3, age = 10, vaccination = true
43+
10
44+
10
45+
10
46+
10 ```
47+
2848
## Prerequisites
2949

3050
You need to install [Nodejs](https://nodejs.org/en/).

build/1-hello-world/helloworld.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/2-first-class/elephant.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/3-bigger-class/elephant2.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/4-more-constructors-and-methods/tiger.js

Lines changed: 93 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/4-more-constructors-and-methods/tiger.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dev/1-hello-world/helloworld.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
function helloworld(message: string) {
2-
console.log("Hello, " + message);
2+
console.log("Hello, " + message)
33
}
4-
helloworld("World!");
4+
helloworld("World!")

dev/2-first-class/elephant.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ class Elephant {
22

33
}
44

5-
let elephant : Elephant = new Elephant();
6-
console.log("elephant object = " + elephant);
5+
let elephant : Elephant = new Elephant()
6+
console.log("elephant object = " + elephant)

dev/3-bigger-class/elephant2.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ class Elephant2 {
5757

5858
}
5959

60-
let elephant2 : Elephant2 = new Elephant2("elephant 2", 10, true);
61-
console.log("elephant => name = " + elephant2.name + ", age = " + elephant2.age + ", vaccination = " + elephant2.vaccination);
60+
let elephant2 : Elephant2 = new Elephant2("elephant 2", 10, true)
61+
console.log("elephant => name = " + elephant2.name + ", age = " + elephant2.age + ", vaccination = " + elephant2.vaccination)
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// you cannot have multiple constructors or overloaded methods like in other
2+
// languages. but you can make this trick with "?" right after parameter
3+
class Tiger {
4+
5+
private _name : string
6+
private _age : number
7+
private _vaccination : boolean
8+
9+
/**
10+
*
11+
* @param _nameParam
12+
* @param _ageParam
13+
* @param _vaccination
14+
*/
15+
constructor(_nameParam : string, _ageParam? : number, _vaccination? : boolean) {
16+
this._name = _nameParam
17+
this._age = _ageParam
18+
this._vaccination = _vaccination
19+
}
20+
21+
get name() : string {
22+
return this._name
23+
}
24+
25+
get age() : number {
26+
return this._age
27+
}
28+
29+
get vaccination() : boolean {
30+
return this._vaccination
31+
}
32+
33+
set name(name : string) {
34+
this._name = name
35+
}
36+
37+
set age(age : number) {
38+
this._age = age
39+
}
40+
41+
set vaccination(vaccination : boolean) {
42+
this._vaccination = vaccination
43+
}
44+
45+
/**
46+
*
47+
* calculateNextVaccination()
48+
* calculateNextVaccination(20)
49+
*
50+
* @param coeficient
51+
* @returns {number}
52+
*/
53+
calculateNextVaccination(coeficient? : number) : number {
54+
if (coeficient == undefined || coeficient == 0) {
55+
return this._age
56+
} else {
57+
return coeficient + 1
58+
}
59+
}
60+
61+
/**
62+
* lambda version (in typescript called narrow function) of calculateNextVaccination with ternary operator
63+
*
64+
* call this like
65+
*
66+
* lambdacalculateNextVaccination()
67+
* lambdacalculateNextVaccination(20
68+
* )
69+
* @param cf
70+
*/
71+
lambdaCalculateNextVaccination = (cf? : number) : number => (cf == undefined || cf == 0) ? this._age : cf + 1
72+
73+
}
74+
let tiger1 : Tiger = new Tiger("Tiger1")
75+
console.log("tiger1 => name = " + tiger1.name + ", age = " + tiger1.age + ", vaccination = " + tiger1.vaccination)
76+
console.log(tiger1.calculateNextVaccination())
77+
console.log(tiger1.calculateNextVaccination(23))
78+
console.log(tiger1.lambdaCalculateNextVaccination())
79+
console.log(tiger1.lambdaCalculateNextVaccination(23))
80+
81+
let tiger2 : Tiger = new Tiger("Tiger2", 14)
82+
console.log("tiger2 => name = " + tiger2.name + ", age = " + tiger2.age + ", vaccination = " + tiger2.vaccination)
83+
console.log(tiger2.calculateNextVaccination())
84+
console.log(tiger2.calculateNextVaccination(12))
85+
console.log(tiger2.lambdaCalculateNextVaccination())
86+
console.log(tiger2.lambdaCalculateNextVaccination(12))
87+
88+
let tiger3 : Tiger = new Tiger("Tiger3", 10, true)
89+
console.log("tiger3 => name = " + tiger3.name + ", age = " + tiger3.age + ", vaccination = " + tiger3.vaccination)
90+
console.log(tiger3.calculateNextVaccination())
91+
console.log(tiger3.calculateNextVaccination(9))
92+
console.log(tiger3.lambdaCalculateNextVaccination())
93+
console.log(tiger3.lambdaCalculateNextVaccination(9))

0 commit comments

Comments
 (0)