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

101
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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