ALAB 308H.2.1 - JavaScript Classes
Learning Objectives
After this lab, learners will have demonstrated the ability to:
- Create a class in JavaScript.
- Create class properties and methods.
- Use the class constructor to set class properties.
- Create instances of a class.
- Invoke class methods.
CodeSandbox
This lab uses CodeSandbox as one of its tools.
If you are unfamiliar with CodeSandbox, or need a refresher, please visit our reference page on CodeSandbox for instructions on:
- Creating an Account
- Making a Sandbox
- Navigating your Sandbox
- Submitting a link to your Sandbox to Canvas
Instructions
- Create a
Vanilla
CodeSandbox and name it "JavaScript Classes Lab." - Follow along with the instructions below, adding to your
index.js
file. - Submit the link to your CodeSandbox on Canvas when you are finished.
Deliverables
- A link to a CodeSandbox that contains your completed
Cat
andPirate
classes with no errors (comment things out if they do not work).
Requirements
You will be working with these classes:
class Cat {}
class Pirate {
constructor() {
}
}
For the Cat
class:
- Give the class at least three properties.
- Give the class at least three methods.
- Create two instances of the class (two cats).
- Log both instances to see their properties.
- Invoke each method from both instances.
For the Pirate
class:
- Create three properties that are set by the constructor.
- Create three methods.
- Instantiate two arrays of three pirates, name the arrays after your favorite pirate ships:
jollyRoger
andblackPearl
. - Loop over each array and print three properties of each pirate.
When trying to think of properties and methods, remember that our classes are nouns, their properties are like adjectives, and their methods are like verbs.