So far, we've been programming separate functions that take some input and convert it to some output. This is a very function-oriented programming style.
Object-Oriented Programming uses objects to model the world. Every thing in our world has information (data) and behaviors (methods) that we can define to represent that thing.
- encapsulation
- state
- behavior
- data properties
- accessor properties
- method
- enumerable
- private data
- factory functions
- What is encapsulation? Why do we use it? How do objects enable it?
- How do objects encapsulate state? How do they encapsulate behavior?
- How do we create private data within JavaScript objects? What is the role of accessor properties?
- What are factory functions and why are they useful?
Do the following in a blank JavaScript file.
- Create an object called
cart
using Object literal syntax ({}
). - Add a
shopper
property to yourcart
object whose value is a string for the name of the shopper. - Add an
items
property to yourcart
object whose value is an empty array. This array will hold values of the form:{ itemName, price }
- Add an
addItem
method to yourcart
object. It should accept two arguments:itemName
andprice
. It should add an object to theitems
array with the form{ itemName, price }
. - Invoke your
cart
obejct'saddItem
method 3 times on yourcart
, adding in the following items:'Apple'
with a price of1
'Banana'
with a price of0.5
'Cherry'
with a price of2.5
- Print your
cart
object'sitem
array to the console. You should see an array with the three item objects that you just added. - Add a
getTotal
method to yourcart
object. It should calculate and return the sum of the prices of all of your cart's items. - Invoke your
cart
object'sgetTotal
method and print the returned value to the console. If you've been following along, it should return4
. - Add a
removeItem
method. It should accept one argument:itemName
. It should find the provided item your cart'sitems
array and remove it if it is found. - Invoke your
cart
object'sremoveItem
method passing in one of your cart's items. - Print your
cart
object'sitems
array to the console. You should NOT see the removed item. - Add a
removeMostExpensiveItem
method. It should find the most expensive item in your cart'sitems
array and remove it. - Invoke your
cart
object'sremoveMostExpensiveItem
method passing in one of your cart's items. - Print your
cart
object'sitems
array to the console. You should NOT see the removed item.
- Encapsulation is the idea that data (properties) and behavior (methods) are bundled together. The behavior interacts with the data in a fixed and predictable interface. Some people refer to it as "data hiding" because data is primarily accessible via methods.
- This
cart
example demonstrates encapsulation because the cart's data and the methods that interact with that data are all bundled in one object.
- This
- State refers to the current values of the properties in an object. It can also refer more broadly to the current values of the variables in a program.
- A factory function is a function that creates an object with a particular set of properties and methods.
- We can create multiple instances of the shopping cart object by invoking the factory function. Each instance will have the same properties and methods.
- Benefits of Factory Functions:
- I don't have to copy/paste the entire object each time I want to make a new instance. I can just invoke the factory function to make a new instance.
- If I want to add a new behavior (method) to carts created by the factory, I can add it one place instead of having to add it to each object individually. Same goes for bugs that may be found in methods. I only need to fix the bug in one place.
- The
this
object is a special variable that holds a different object depending on where you reference it. Within object methods, it is the object that the method is being called on.
const obj = {
getThis() {
return this;
}
}
console.log(obj === obj.getThis()); // prints true
console.log(this);
- All of this is a process of abstraction. We create these objects with data and methods once so that in the future, we can work with the more abstract object's methods to interact with the data inside.