Exercise: JS Object-Oriented Programming
#
ObjectiveYour objective is to practice OOP concepts and JS class notation syntax.
#
Steps#
Getting StartedExercise Repo: JS Object Oriented Programming
- Open your command line and navigate to your
repos
directory (if you do not have arepos
folder, then you can usemkdir repos
to create one) - Use this template repository to start a new project in your repos folder:
git clone <repo_name>
- cd
repo_name
to navigate into your new repo directory - Start Visual Studio Code and select 'Open Folder'. Then select
repo_name
to open the folder in the editor (or just typecode .
in your terminal inside the repo directory) - Follow the instructions on the README.md file to complete exercises
- Open the app.js file to get started
#
Exercise 1- Expanding from our exercise example during the lesson, create a parent class
class Person {}
with properties forname
,pets
,residence
, andhobbies
. The Person class will also have a methodinfo()
andsoundOff()
.
- Use class notation to create a class named
Person
- Declare the
constructor()
method with parametersname
(string),pets
(number),residence
(string), andhobbies
(array) allowed to be passed in - The
constructor()
method assignsthis.
properties to each parameter - Declare an
addHobby()
method on the class that takes in a newhobby
(string), and adds it to the object'shobbies
array property - Declare a
removeHobby()
method on the class that takes in ahobby
(string), and removes the hobby from the object'shobbies
array property - Declare a
greeting()
method thatconsole.log
's a generic greeting for a Person... ex:greeting() {console.log("Hello fellow person!")}
#
Exercise 2Now we'll create a subclass Coder
that inherits from our Person
class
- Use class notation to create a class named
Coder
that inherits from thePerson
class - Declare the
constructor()
method with parametersname
(string),pets
(number),residence
(string), andhobbies
(array) allowed to be passed in - Call the
super()
method inside theconstructor
method and pass in the given parameters - Still inside the
constructor
body, assignthis.occupation
as"Full Stack Web Developer"
- Override the
greeting()
method toconsole.log
a custom greeting from a coder...
#
Exercise 3Let's create instances of our classes
- Create a variable and assign a Person object to it using the
new
keyword followed by the classconstructor
- Create a variable and assign a Coder object to it using the
new
keyword followed by the classconstructor
- Call the object methods and
console.log
the object properties to test your work
#
Exercise 4In this final exercise, we'll create a class that has the functionality of a basic calculator.
- Create a class called
Calculator
- Initialize a
result
property within the calculatorconstructor
with an initial value of0
- Declare methods on the class that represent basic arithmetic:
add()
,subtract()
,multiply()
,divide()
- These methods should currently take in 2 parameters, and assign the result of the appropriate arithmetic based on the method name to the object's
result
property - Should return the result
- EXTRA: if only one value is passed in, use the object's current
result
value as the first value in the operation
- Declare a method called
displayResult()
that willconsole.log
the result property stored on the object - Test your work by instantiating an object from your class (using the class constructor), and calling some of the calculator methods