I keep not understanding some fundamentals about classes.
Here's a fake example illustrating a problem I keep facing and I'm never sure how to properly solve it:
class FlyingSaucer {
constructor(){
this.velocity = {x:0, y:0}
this.isMoving = false
}
move(){
if (inventory.load < 125){ //<-looking up Inventory class object
this.velocity = {x: 1, y:0}
} else {
this.velocity = {x: 0.5, y:0}
}
this.isMoving = true
}
beamUp(victim){
if(victim.species === 'cow') {
inventory.cows += 1 //<-changing Inventory object
}
inventory.load += victim.weight//<-changing Inventory object
cow.freakOut() //<-calling a method of Creature class object
}
}
class Inventory{
constructor(){
this.cows = 5
this.humans = 1
this.load = 0
}
}
class Creature{
constructor(species, weight) {
this.species = species
this.weight = weight
this.sounds = sounds
this.isScared = false
}
freakOut(){
this.isScared = true
}
}
const saucer = new FlyingSaucer()
const inventory = new Inventory()
const cow = new Creature('cow', '25')
Now, I expect that I should somehow pass these objects to each other after creating them. But here I'm wondering what are my options?