Skip to content

Commit da9d053

Browse files
committed
3. bigger class. Class with constructor, atributes with access protection, getters and setters.
1 parent c382496 commit da9d053

File tree

5 files changed

+122
-7
lines changed

5 files changed

+122
-7
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
**output** : ```Hello, World! ```
2323
- **2. first class** (simple class)
2424
**output** : ```elephant object = [object Object] ```
25-
25+
- **3. bigger class** : (getters, setters, constructor with params, access keywords, )
26+
**output** : ```elephant => name = elephant 2, age = 10, vaccination = true ```
2627

2728
## Prerequisites
2829

build/3-bigger-class/elephant2.js

Lines changed: 58 additions & 0 deletions
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 & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dev/3-bigger-class/elephant2.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class Elephant2 {
2+
3+
// access keywords, object atribute (property)
4+
private _name : string
5+
6+
private _age : number
7+
8+
private _vaccination : boolean
9+
10+
11+
/**
12+
*
13+
* if constructor parameter is public you dont have to declare it in class, is already
14+
* visible like this._name but vidible also outside class scope !!
15+
*
16+
* @param _nameParam
17+
* @param _ageParam
18+
* @param _vaccination
19+
*/
20+
constructor(_nameParam : string, _ageParam : number, _vaccination : boolean) {
21+
this._name = _nameParam
22+
this._age = _ageParam
23+
this._vaccination = _vaccination
24+
}
25+
26+
/**
27+
* getter example
28+
* @returns {string}
29+
*/
30+
get name() : string {
31+
return this._name
32+
}
33+
34+
get age() : number {
35+
return this._age
36+
}
37+
38+
get vaccination() : boolean {
39+
return this._vaccination
40+
}
41+
42+
/**
43+
* setter example
44+
* @param name
45+
*/
46+
set name(name : string) {
47+
this._name = name
48+
}
49+
50+
set age(age : number) {
51+
this._age = age
52+
}
53+
54+
set vaccination(vaccination : boolean) {
55+
this._vaccination = vaccination
56+
}
57+
58+
}
59+
60+
let elephant2 : Elephant2 = new Elephant2("elephant 2", 10, true);
61+
console.log("elephant => name = " + elephant2.name + ", age = " + elephant2.age + ", vaccination = " + elephant2.vaccination);

dev/bigger-class/elephant2.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)