Skip to content

Commit c10946a

Browse files
author
Max Kelly
committed
101. Getters & Setters
1 parent dca5819 commit c10946a

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

Diff for: hangman/app.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ const puzzleEl = document.querySelector("#puzzle"); // Selects the id element in
33
const statusEl = document.querySelector('#status')
44
const game1 = new Hangman("Cat", 2);
55

6-
puzzleEl.textContent = game1.getPuzzle();
7-
statusEl.textContent = game1.getStatusMessage()
6+
puzzleEl.textContent = game1.puzzle;
7+
statusEl.textContent = game1.statusMessage
88

99
window.addEventListener("keypress", function(e) {
1010
const guess = String.fromCharCode(e.charCode);
1111
game1.makeGuess(guess);
12-
puzzleEl.textContent = game1.getPuzzle();
13-
statusEl.textContent = game1.getStatusMessage()
12+
puzzleEl.textContent = game1.puzzle;
13+
statusEl.textContent = game1.statusMessage
1414
});
1515

1616

Diff for: hangman/hangman.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// No guesses? -> ***
22
// Guessed "c", "b" and "t"? -> c*t
3+
34
class Hangman {
45
constructor(word, remainingGuesses) {
56
this.word = word.toLowerCase().split("");
@@ -21,7 +22,7 @@ class Hangman {
2122
}
2223
}
2324

24-
getStatusMessage() {
25+
get statusMessage() {
2526
if (this.status === "Playing") {
2627
return `Guesses left: ${this.remainingGuesses}`;
2728
} else if (this.status === "Failed") {
@@ -31,7 +32,7 @@ class Hangman {
3132
}
3233
}
3334

34-
getPuzzle() {
35+
get puzzle() {
3536

3637
let puzzle = "";
3738

Diff for: oop/get-set.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//----- Getter & Setters -----
2+
3+
const data = {
4+
locations: [],
5+
get location() {
6+
return this._location
7+
},
8+
set location(value) {
9+
this._location = value.trim()
10+
this.locations.push(this._location)
11+
}
12+
}
13+
14+
// Code that uses the data object above
15+
data.location = ' Melbourne '
16+
data.location = ' New York'
17+
console.log(data)

Diff for: oop/person.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ class Person {
2323
return bio;
2424
}
2525

26-
setName(fullName) {
27-
const names = fullName.split(" "); // This .split allows us to split a string and make an array. We're currently splitting on a space. This would create just Andrew
26+
set fullName(fullName) {
27+
const names = fullName.split(' '); // This .split allows us to split a string and make an array. We're currently splitting on a space. This would create just Andrew
2828
this.firstName = names[0];
2929
this.lastName = names[1];
3030
}
31+
get fullName () {
32+
return `${this.firstName} ${this.lastName}`
33+
}
3134
}
3235

3336
// The class Employee extends from Peron meaning we it has the same behaviour as person. This means we don't have to duplicate code.
@@ -38,7 +41,7 @@ class Employee extends Person {
3841
}
3942
getBio() {
4043
// Max is a Helicopter Pilot
41-
return `${this.firstName} ${this.lastName} is a ${this.position}.`
44+
return `${this.fullName} is a ${this.position}.`
4245
}
4346
getYearsLeft() {
4447
return 65 - this.age
@@ -67,10 +70,10 @@ class Student extends Person {
6770
}
6871
}
6972

70-
const me = new Student('Max', 'Kelly', 21, 10, 'English')
71-
console.log(me.getBio())
72-
me.updateGrades(+80);
73+
const me = new Employee('Max', 'Kelly', 21, 'CEO', 'English')
74+
me.fullName = 'Alex John'
7375
console.log(me.getBio())
76+
//me.updateGrades(+80);
7477

7578
// ---------------------
7679
// The below creates a new person and logs it to the console.

0 commit comments

Comments
 (0)