Exercise: JS Object-Oriented Programming
Objective#
Your objective is to practice OOP concepts and JS class notation syntax.
Steps#
Getting Started#
Exercise Repo: JS Object Oriented Programming
- Open your command line and navigate to your
reposdirectory (if you do not have areposfolder, then you can usemkdir reposto create one) - Use this template repository to start a new project in your repos folder:
git clone <repo_name> - cd
repo_nameto navigate into your new repo directory - Start Visual Studio Code and select 'Open Folder'. Then select
repo_nameto 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'shobbiesarray property - Declare a
removeHobby()method on the class that takes in ahobby(string), and removes the hobby from the object'shobbiesarray property - Declare a
greeting()method thatconsole.log's a generic greeting for a Person... ex:greeting() {console.log("Hello fellow person!")}
Exercise 2#
Now we'll create a subclass Coder that inherits from our Person class
- Use class notation to create a class named
Coderthat inherits from thePersonclass - Declare the
constructor()method with parametersname(string),pets(number),residence(string), andhobbies(array) allowed to be passed in - Call the
super()method inside theconstructormethod and pass in the given parameters - Still inside the
constructorbody, assignthis.occupationas"Full Stack Web Developer" - Override the
greeting()method toconsole.loga custom greeting from a coder...
Exercise 3#
Let's create instances of our classes
- Create a variable and assign a Person object to it using the
newkeyword followed by the classconstructor - Create a variable and assign a Coder object to it using the
newkeyword followed by the classconstructor - Call the object methods and
console.logthe object properties to test your work
Exercise 4#
In this final exercise, we'll create a class that has the functionality of a basic calculator.
- Create a class called
Calculator - Initialize a
resultproperty within the calculatorconstructorwith 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
resultproperty - Should return the result
- EXTRA: if only one value is passed in, use the object's current
resultvalue as the first value in the operation
- Declare a method called
displayResult()that willconsole.logthe 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