Home > database >  Create Static Method to Call Method within Class for each Member of Class
Create Static Method to Call Method within Class for each Member of Class

Time:10-21

I'm getting "Person.introduce" is not a function with the following code.

Essentially, what I am trying to do is:

Create a static method called introducePeople that will take in an array of Person instances.

  • Have the method console.log an error message of "introducePeople only takes an array as an argument." if the argument is not of type Array.
  • Have the method console.log an error message of "All items in array must be Person class instances." if any of the items in the array are not instances of the Person class.
  • If there are no errors logged to the console, call introduce on each of the People instances in the input array.
class Person{

  firstName;
  lastName;
  age;

  constructor(firstName, lastName, age) {
    this.firstName = firstName,
    this.lastName = lastName,
    this.age = age
  }

  introduce() {
    console.log(`Hi, I'm ${this.firstName} ${this.lastName}, and I'm ${this.age} years old.`);
  }


  static introducePeople(...people) {
    if (Array.isArray(people)) {
      console.error("introducePeople only takes an array as an argument.")
    } else if (!(people instanceof Person)) {
      console.error("All items in array must be Person class instances.")
    } else {
      return people
        .forEach((aPerson) => Person.introduce(aPerson))
    }
  }


}; 

I've also tried:

static introducePeople(...people) {
    if (!Array.isArray(people)) {
      console.log("introducePeople only takes an array as an argument.")
    } else if (!(people instanceof Person)) {
      console.log("All items in array must be Person class instances.")
    } else {
      return people
        .forEach((aPerson) => aPerson.introduce)
    }
  }

Which takes away the aforementioned error, however, the code does not work the way it seems it should.

Lastly, I've tried this:

static introducePeople(array) {
    for (const person of array) {
      if (person instanceof Person) {
        person.introduce();
      } else if (!(Array.isArray(array))) {
        console.error("introducePeople only takes an array as an argument.")
        return;
      } else {
        console.error("All items in array must be Person class instances.")
        return;
      }(!(person instanceof Person))
    }
  }

Which is good but it does not pass the "Must provide error message if array is not array/person is not instance of person"

CodePudding user response:

You're calling the introduce method as a static method, but introduce is an instance method that is given to each Person instance. Maybe you meant to call introduce on every person in the people array.

Your if statements won't work as expected. ...people is always an array, because you explicitly said it would be with the spread operator .... Thats why the if and else if will never be true.

Instead loop over the people array and check if each item in the array is an instance of Person. If it is, then call the introduce method on the person object.

class Person {
  firstName;
  lastName;
  age;

  constructor(firstName, lastName, age) {
    this.firstName = firstName,
    this.lastName = lastName,
    this.age = age
  }

  introduce() {
    console.log(`Hi, I'm ${this.firstName} ${this.lastName}, and I'm ${this.age} years old.`);
  }

  static introducePeople(people) {
    if (!Array.isArray(people)) {
      throw new Error('introducePeople only takes an array as an argument.');
    }
    
    const hasValidArguments = people.every(
      person => person instanceof Person
    );
    
    if (!hasValidArguments) {
      throw new Error('All items in array must be Person class instances.');
    }
    
    for (const person of people) {
      if (person instanceof Person) {
        person.introduce();
      }
    }
  }
};

const buzz = new Person('Buzz', 'Aldrin', 46);
const neil = new Person('Neil', 'Armstrong', 42);

Person.introducePeople([buzz, neil]);

CodePudding user response:

Maybe you should use

 .forEach((aPerson) => aPerson.introduce())

because introduce is not a static function;

Besides, I believe the people param of introducePeople will always be an array, even called without params, it will be []

see MDN rest parameters

  • Related