Last active
November 4, 2020 02:17
-
-
Save Dangeranger/27855d6395d3ceec30f32adeec9ceee6 to your computer and use it in GitHub Desktop.
Code example from class on 2020-11-03, for showing objects from classes working together
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Room { | |
constructor() { | |
console.log('making the room'); | |
this.secretItem = { note: 'in the dresser' }; | |
this.key = 'painting hall piece'; | |
} | |
| |
look(awareness) { | |
console.log('as you look around the room.....'); | |
console.log({ awareness }); | |
if (awareness.state === 'awake') { | |
console.log('someone is awake'); | |
console.log(`The secret item is ${this.secretItem.note}`); | |
} else if (awareness.state === 'sleeping') { | |
console.log('someone is asleep'); | |
console.log(`The secret key is the ${this.key}`); | |
} else { | |
console.log('unknown state of awareness'); | |
} | |
} | |
} | |
| |
class Awareness { | |
constructor(defaultState = 'awake', nextStates = ['sleeping', 'groggy']) { | |
this.state = defaultState; | |
this.nextStates = nextStates; | |
this.defaultState = defaultState; | |
} | |
| |
flip() { | |
if (this.state === this.defaultState) { | |
this.state = this.nextStates[0]; | |
} else { | |
this.state = this.nextStates[1]; | |
} | |
return this.state; | |
} | |
} | |
| |
let nextStates = ['full', 'sleeping']; | |
const mealTime = new Awareness('hungry', nextStates); | |
| |
const room = new Room(); | |
const awareness = new Awareness(); | |
room.look(awareness); | |
awareness.flip(); | |
room.look(awareness); | |
awareness.flip(); | |
room.look(awareness); | |
console.log('DONE!!!'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment