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

102
Vistas
association between two classes

i want to call the virable and its value in class from other class example : if i create a class Doctor with one attribue the name of doctor(Name)

and i create another class of practice_doctor with one attribue too the name of practice doctor(p_name)

and in the class of practice doctor i create a void function that print the name of practice doctor and the name of the doctor that practice him in the class doctor.

in the main methond i create in instance of doctor class and assign him a name "jack" and create in instance of practice doctor class and assign him an name such as a "mick" so the function print info must print the name of the doctor and the name of practice doctor such as "the doctor jack is practice doctor mick"

how can i association between too classes(doctor and practice doctor)?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The next provided example code is a sophisticated guess about what the OP might want to achieve.

There is a base class Doctor and a sub class PracticeDoctor which extends the former base class. And the association the OP is talking about is not in between classes (at this level there is no other association than the inheritance chain of prototypes).

But the OP might have meant the association of two types (doctor instances). In this case a PracticeDoctor instance would aggregate another Doctor or PracticeDoctor instance/type as e.g. its own associate property.

class Doctor {
  constructor(
    // assure default `name` value.
    name = 'unkown',
  ) {
    this.name = name;
  }
}

class PracticeDoctor extends Doctor {
  constructor(
    // default value handling via super.    
    name,
    // assure default `associate` value.
    associate = new Doctor,
  ) {    
    // `name` initialization/assignement
    // via base class super call.
    super(name);
    
    // aggregation of another Doctor type.
    this.associate = associate;
  }
  teamedUpWith () {
    return [
      this.constructor.name,
      ' ',
      this.name,
      '\'s associate is ',
      this.associate.constructor.name,
      ' ',
      this.associate.name
    ].join('')
  } 
}

const drJack = new Doctor('Jack');
const drJill = new PracticeDoctor('Jill');
const drMick = new PracticeDoctor('Mick', drJack);
const drJess = new PracticeDoctor('Jess', drJill);

console.log({ drJack, drJill, drMick, drJess });
console.log(
  'drJack?.teamedUpWith?.() ...',
  drJack?.teamedUpWith?.()
);
console.log(
  'drJill.teamedUpWith() ...',
  drJill.teamedUpWith()
);
console.log(
  'drMick.teamedUpWith() ...',
  drMick.teamedUpWith()
);
console.log(
  'drJess.teamedUpWith() ...',
  drJess.teamedUpWith()
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar
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