Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

79
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda