-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetters.js
More file actions
33 lines (26 loc) · 715 Bytes
/
getters.js
File metadata and controls
33 lines (26 loc) · 715 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const person = {
_firstName: 'John',
_lastName: 'Doe',
get fullName() {
if (this._firstName && this._lastName){
return `${this._firstName} ${this._lastName}`;
} else {
return 'Missing a first name or a last name.';
}
}
}
// To call the getter method:
console.log(person.fullName); // 'John Doe'
// Example
const robot = {
_model: '1E78V2',
_energyLevel: 100,
get energyLevel(){
if(typeof this._energyLevel === 'number'){
return 'My current energy level is '+this._energyLevel;
}else{
return 'System malfunction: cannot retrieve energy level';
}
}
};
console.log(robot.energyLevel);