简体   繁体   中英

Question on how to work with class instances inside an array

So i am learning how to work with classes and class instances and have created a scenario where i have a class Employee that is extended by three other classes that represent employees from different departments. I already managed to create a function that allows me to create a new instance of any class and push it into an array containing all existing employees.

So now i am trying to play around and iterate over the array and create new functions that allow me to access a specific value of any instance.

for example the first one i am trying to do is a function that return true or false if an employee is working remotely:

function areTheyRemote(employee){
    if (employee.workplace === 'home'){
        return true;
    } else {
        return false;
    }
}

I have no idea and couldn't find any answers online on how to do it, hoping you could shed a light on me. Cheers

Without seeing your class code for the employee it should be something like this:

 function Employee(name, age, workplace) { this.name = name this.age = age this.workplace = workplace } const bob = new Employee('bob', 22, 'remote') function areTheyRemote(employee) { if (employee.workplace === 'remote') { return true; } else { return false; } } console.log(areTheyRemote(bob))

After some more digging i found that my major error was not passing as argument the class. I capitalized "employee" as class syntax and it now runs as intended.

Also simplified the previous function to create new class instances, so thank you for the comments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM