In this exercise, you will be creating a class by creating a constructor and defining static and instance methods.
Clone the starter from the Download link at the bottom of this page.
Run npm install to install any dependencies.
Implement the following in the classes/person.js file.
Create a class Person that has the following:
-
instance variables that include
firstName,lastName, andage -
an instance method called
introducethat will introduce the person by usingconsole.logwith a string saying, "Hi, I'm<firstName><lastName>, and I'm<age>years old.". -
a static method called
introducePeoplethat will take in an array ofPersoninstances.Have
introducePeoplethrow an Error with a message of "introducePeople only takes an array as an argument." if the argument is not of typeArray.Have
introducePeoplethrow an Error with a message of "All items in array must be Person class instances." if any of the items in the array are not instances of thePersonclass.If no Errors are thrown then
introducePeopleshould callintroduceon each of thePeopleinstances in the input array.
Tip: We'll dive into
Errorsin more detail later. For now, just know that anErroris also aClass, and when you throw a new error the first argument will be that error's message. Use MDN's Error Examples and Error Constructor to help you out if you're stuck.
Run the test specs in the test/person-spec.js file to test that you
have created the Person class correctly:
npm test