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

251
Views
Object Oriented Programming Syntax Question, passing a class method from one file to another

I have two files, employee.js

const Manager = require('./manager.js');


let manager = new Manager();

class Employee {
    constructor(name, title, salary, boss=null)
    {
        this.name = name;
        this.title = title;
        this.salary = salary;
        this.boss = boss;
        manager.addEmployee(this.Employee);
    }
}




module.exports = Employee;

And manager.js

const Employee = require('./employee.js');

class Manager extends Employee {
    constructor(name, salary, title, boss=null) {
        // this.name = name;
        // this.title = title;
        // this.salary = salary;
        // this.boss = boss;
        super(name, salary, title, boss);
        this.managedEmployees = [];
    }

    addEmployee(Employee) {
        this.managedEmployees.push(Employee);
    }
}

let annie = new Manager('annie', 100000, 'Director');
let alvy = new Employee('alvy', 75000, 'Analyst', annie);

console.log(annie);

module.exports = Manager;

I am desperately trying to find what syntax would allow me to take the addEmployee(Employee) method and call it in the constructor of the Employee class. I want Manager to be the child of Employee and have an array of all the employees under the manager. The method is pushing into the array but I cannot figure out what I should be doing for console.log(annie); to end up with this input

<ref *1> Manager {
  name: 'Annie',
  salary: 100000,
  title: 'Director',
  manager: null,
  employees: [
    Employee {
      name: 'Alvy',
      salary: 75000,
      title: 'Analyst',
      manager: [Circular *1]
    }
  ]
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Setup your Employee class like this:

    class Employee {
        constructor(name, title, salary, boss=null)
        {
            this.name = name;
            this.title = title;
            this.salary = salary;
            this.boss = boss;
            if (boss) {
                boss.addEmployee(this);
            }
        }
    }

You're passing in an instanceof Manager, so if it's not null you can call a method on that. Notice that you want to just pass this as the input to addEmployee, as this.Employee is trying to access a property or method in that class which doesn't exist.

Console output will look something like this:

    Manager {
        name: "annie",
        title: 100000,
        salary: "Director",
        boss: null,
        managedEmployees: [
            Employee {
                name: "alvy",
                title: 75000,
                salary: "Analyst",
                boss:Manager {...}
            }
        ]
    }

You will also notice that you have the order of parameters swapped for title/salary on your Employee constructor.

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!