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

249
Visualizações
What should the output of a JavaScript Class look like?

I am attempting to use inquirer to collect user input, that is turned into an object, it is then passed into a class constructor. Everything seems to working the way I want it to accept the resulting array of objects after they are passed though the classes come out in a way that is confusing me,

when I console.log the array that contains the objects that are returned by my classes this is what comes out:

[
  Manager {
    name: 'bob',
    id: '24',
    email: 'bob.com',
    officeNumber: '1'
  },
  Engineer {
    name: 'jack',
    id: '347',
    email: 'jack.com',
    github: 'jackolantern'
  },
  Intern {
    name: 'sally',
    id: '987',
    email: 'sally.com',
    school: 'UCF'
  }
]

Here is an example of one of the classes:

class Manager extends Employee {
  constructor (employeeObj) {
    super (employeeObj);
    this.name = employeeObj.name;
    this.id = employeeObj.id;
    this.email = employeeObj.email;
    this.officeNumber = employeeObj.officeNumber;

  }
  getOfficeNumber () {
    return this.officeNumber;
  }
  getRoll () {
    return "Manager";
  }
}
module.exports = Manager;

and this is how the objects are passed into the classes:

const prompts = async () => {
  let employeeObj = {};
  const employee = await inquirer.prompt(addEmployee);

    switch(employee.addEmployee){
      
      case 'Manager':
       employeeObj = await managerQuestions();
      const manager = new Manager(employeeObj);
        output.push(manager);
        if(employeeObj.addAnother){
          return prompts();
        } else {
          complete();
        }
        break;

      case 'Engineer':
       employeeObj = await engineerQuestions();
        const engineer = new Engineer(employeeObj)
        output.push(engineer)
        if(employeeObj.addAnother){
          return prompts();
        } else {
          complete();
        }
        break;

      case 'Intern':
        employeeObj = await internQuestions();
        const intern = new Intern(employeeObj)
        output.push(intern)
        if(employeeObj.addAnother){
          return prompts();
        } else {
          complete();
        }
        break;

      default:
        console.log('you have reached the default switch statement. thant should not happen. Please try again!');
      }
  }

what I cant seem to figure out, is why the "roll" for each object (Manager, Engineer, Intern) is being placed outside the corresponding object and not inside it. I was able to add a this.roll = "manager" inside the constructor and as expected it added in a property called roll with a value of "manager" which will work just fine for what I need to do, but how do I get rid of that Manager, Engineer, and Intern that shows up before each object in the output array, or can it be moved inside the object?

thank you for taking the time to read through all this.

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