OOP
Lesson Objectives
Explain why we need classes
Create a class to define the blueprint for creating objects
Add methods to a class
Set properties on an instance of a class
What is
this
? Why do we need it?Make an instance of each class customizable
Create methods to alter the properties of an instance
Explain why we need classes
Sometimes we need to repetitively create new objects with the same attributes. Imagine we wanted to create a bunch of hotels for a boutique travel agency.
We'd need at least:
name
location
rating
vacancies
tags describing the hotel
rooms (an array of objects with details of the rooms)
Great! One object. How can we create another one? How about copy pasting, then changing all the details? Typing it all from scratch? What if someone makes a typo with a key? What if our boutique expands to 1000 hotels?
There is a better way! We can create a class, which will be a blueprint or template for similar objects. Not only can we add data, we can also include functionality.
Create a class to define the blueprint for creating objects
When creating a class, it's custom to capitalize the first letter of the variable, so we know it's a class
. This follows customs in other programming languages.
Now we can "instantiate" or create new objects using this class. We do this by adding the new
keyword before calling the class name like a function.
Add methods to a class
Right now, our object doesn't do anything. Let's have it do some stuff;
These methods can, of course, take parameters:
We only had to update our code in one place, and then every instance of the class now has the updated functionality. Sweet!
If we have multiple methods, don't put commas between them:
Set properties on an instance of a class
If we log the instances of our class, we'll see they don't have any properties:
Let's add some properties with a constructor
function. This is a function that gets called once, each time an object is created:
constructor
is a special function. Try misspelling constructor
(ie constr
) and see if you still get the same results.
What is this
?
this
?What is this
? Let's think back to our hw problem on making a vending machine and let's build it again together.
Model a vending machine
a vending machine is an object
it has an array of snacks (make 3 snacks)
snacks are objects that have a name and a price
a vending machine has a function vend that allows user to enter the array position (a number) of the snack and then that snack will be returned
Be able to call vendingMachine.vend() with a valid integer to return a snack
When we wanted to access snacks within our object we had to put the name of the object vendingMachine
in it to access the snacks
This worked just fine, because we knew what the name of the object would be.
But now we are making new objects that can be named anything. So we need a way to say this
object's snacks or this
object's legs property - We need a pronoun, a generic term to refer to whatever the name of the object is.
JavaScript uses this
. So we can access things within an object this way. We can update our vendingMachine to use this
instead:
Make an instance of each class customizable
Our world is very boring and weird if all of our people are exactly the same! We need a way to customize each object: Our constructor function can take params which we can use to alter the properties of the object instantiated. This allows us to customize each instance:
Create default values
Sometimes, you want to create default values that can be overwritten.
There are two ways to write it, writing it in the constructor with an =
is the newer way. Using ||
is the older way and does work. In both cases, values that have default parameters should go at the end (why?).
Create methods to alter the properties of an instance
We can of course, alter the properties of an instance, after it is created:
But it's a nice practice to define a method that will alter that:
This way, everything is done with methods
Other developers can quickly scan the class definition to determine what you'd like them to be able to do with the class
Objects interacting with other objects
We can pass an object to another object to have them interact
Adapted from SEI-MAE
Last updated
Was this helpful?