1

The Javascript code below outputs: Employee {#name: "sheila", #gender: "female", #manager: Employee, #job: Job}

How do I fix it so that it outputs: Employee {#name: "sheila", #gender: "female", #manager: "pierre", #job: "software engineer"}

Not sure if there is a hoisting problem. Which lines do I fix so that #manager is "pierre" and #job is "software engineer"?

class Person{                   
    #name;              
    #gender;                

    constructor(name, gender) {             
        this.#name = name;          
        this.#gender = gender;          
    };              
                    
    setName(name) {             
        this.#name = name;          
    };              
                    
    getName() {             
        return this.#name;          
    };              
                    
    setGender(gender) {             
        this.#gender = gender;          
    }               
                    
    getGender() {               
        return this.#gender;            
    };              
}                   
                    
class Employee extends Person{                  
    #manager;               
    #job;               
                    
    constructor(name, gender, manager, job) {               
        super(name, gender);            
        this.#manager = manager;            
        this.#job = job;            
    };              
                    
    setManager(manager) {               
        this.#manager = manager;            
    }               
                    
    getManager() {              
        return this.#manager;           
    }               
                    
    setJob(job) {               
        this.#job = job;            
    }               
                    
    getJob() {              
        return this.#job;           
    }               
}                   
                    
class Job {                 
    #title;             
                    
    constructor(title) {                
        this.#title = title;            
    };              
}                   
                    
const softwareEngineer = new Job("software engineer");                  
                    
const keith = new Employee("keith", "male");                    
                    
const pierre = new Employee("pierre", "male");                  
                    
const sheila = new Employee("sheila", "female", keith, softwareEngineer);                   
                    
sheila.setManager(pierre);                  
console.log(sheila);                    
1
  • 1
    Well the "job" property is an object. You can give it a .toString() method so that when it's logged it can produce a string to your liking. Commented Dec 7, 2020 at 3:54

1 Answer 1

2

The issue is that you print the type of the Object instead of the value. You need a toString function for Job and Manager to print the value instead of type, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.