Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

78
Views
How to structure classes in a way that they can work together?

I keep not understanding some fundamentals about classes.

  1. What factors influence whether we should make a few different classes or one big class? Is it just about preference?
  2. How can I make class instances "communicate" with each other? (example below)

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?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!