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
introduce
that will introduce the person by usingconsole.log
with a string saying, "Hi, I'm<firstName>
<lastName>
, and I'm<age>
years old.". -
a static method called
introducePeople
that will take in an array ofPerson
instances.Have
introducePeople
throw an Error with a message of "introducePeople only takes an array as an argument." if the argument is not of typeArray
.Have
introducePeople
throw 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 thePerson
class.If no Errors are thrown then
introducePeople
should callintroduce
on each of thePeople
instances in the input array.
Tip: We'll dive into
Errors
in more detail later. For now, just know that anError
is 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