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

100
Views
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 answers
Answer question

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 Report
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!