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

260
Vistas
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 Respuestas
Responde la pregunta

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