Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

80
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda