#100Devs - Javascript OOP (cohort 2)

Class 30 of our Free Web Dev Bootcamp for folx affected by the pandemic. Join live T/Th 6:30pm ET leonnoel.com/twitch and ask questions here: leonnoel.com/discord

Skip to first slide
Slide 1 of 87

 

Javascript - OOP

Leon Noel

#100Devs

And I was thinking 'bout starting up my own school
A Montessori
And the hallway looking like a monastery, oh yes
I'm way up, I feel blessed


Slide 2 of 87

Agenda


Slide 3 of 87

Questions

About last class or life


Slide 4 of 87

Checking In

Like and Retweet the Tweet

!checkin


Slide 5 of 87

Trough Of Sorrow


Slide 6 of 87

No Networking Until May


Slide 7 of 87

Client Deadline: March 5th


Slide 8 of 87

Client Alternatives


Slide 9 of 87

Grassroots Volunteer*


Slide 10 of 87

Free Software / Open Source

https://www.firsttimersonly.com/


Slide 11 of 87

Live Crafting Your Story

FRIDAY
6:30pm EST


Slide 12 of 87

OFFICE HOURS

SUNDAY
1:00pm EST


Slide 13 of 87

Objects

What are objects?


Slide 14 of 87

USE
UNDERSTAND
BUILD


Slide 15 of 87

Finna Make Sum Nerds Angry


Slide 16 of 87

Objects


Slide 17 of 87

Think of a physical object

What are it's attributes and behaviors? 


Slide 18 of 87

How about a stopwatch

What are its attributes and behaviors? 


Slide 19 of 87

Stopwatch Object

  • Properties (attributes):
    Might contain a variable to represent hours, another to represent minutes, and another to represent seconds. 

 

 


Slide 20 of 87

Stopwatch Object

let stopwatch = {}

stopwatch.currentTime = 12

stopwatch.tellTime = function(time){
  console.log(`The current time is ${time}.`)
}

stopwatch.tellTime(stopwatch.currentTime)

Notation?


Slide 21 of 87

Objects

What if we want to make

a lot of objects?

How much money you got? How many problems you got? How many people done doubted you? Left you out to rot?


Slide 22 of 87

Car Factory?

Constructors then Classes


Slide 23 of 87

Car Factory

Constructor

function MakeCar(carMake,carModel,carColor,numOfDoors){
  this.make = carMake
  this.model = carModel
  this.color = carColor
  this.doors = numOfDoors
  this.honk = function(){
    alert('BEEP BEEP FUCKER')
  }
  this.lock = function(){
    alert(`Locked ${this.doors} doors!`)
  }
}

let hondaCivic = new MakeCar('Honda','Civic','Silver', 4)

let teslaRoadster = new MakeCar('Tesla','Roadster', 'Red', 2)

Slide 24 of 87

Car Factory

We forgot enable bluetooth! 

let teslaRoadster = new MakeCar('Tesla','Roadster', 'Red', 2)

console.log( teslaRoadster.bluetooth )  //undefined

MakeCar.prototype.bluetooth = true

console.log( teslaRoadster.bluetooth ) //true 

A prototype is another object that is used as a fallback source of properties


Slide 25 of 87

Car Factory

Why does .toString() work?!?

let teslaRoadster = new MakeCar('Tesla','Roadster', 'Red', 2)

console.log( teslaRoadster.doors.toString() )  // "2" not 2

A prototype is another object that is used as a fallback source of properties


Slide 26 of 87

Car Factory

Look Ma! New syntax!

class MakeCar{
  constructor(carMake,carModel,carColor,numOfDoors){
    this.make = carMake
    this.model = carModel
    this.color = carColor
    this.doors = numOfDoors
  }
  honk(){
    alert('BEEP BEEP FUCKER')
  }
  lock(){
    alert(`Locked ${this.doors} doors!`)
  }
}

let hondaCivic = new MakeCar('Honda','Civic','Silver', 4)

let teslaRoadster = new MakeCar('Tesla','Roadster', 'Red', 2)

Classes are like templates for objects!


Slide 27 of 87

Let's Code

Objects - Espresso Machine


Slide 28 of 87

BUT WHY?

Why Use Objects?
Why Would We Need A Factory?


Slide 29 of 87

As our codebase gets larger and more folx join the team, can we keep our code organized?


Slide 30 of 87

Is it easy to add new features and functionality?


Slide 31 of 87

Can another developer look at my code and understand what is happening?


Slide 32 of 87

Can I make changes without losing sleep at night? 


Slide 33 of 87

What if there was a system, a paradigm, a set of rules, an agreed upon way to structure our code that:

 

Made it easier to add new stuff

Made it easier to read through what was already coded

And made it so you were not afraid to make changes

 

Music & Light Warning - Next Slide


Slide 34 of 87

OOP BABY


Slide 35 of 87

OBJECT ORIENTED PROGRAMING 


Slide 36 of 87

Let's See Some Code


Slide 37 of 87
let hourlyRate = 250
let hours = 160
let taxRate = .35

function calculateProfit(rate, numOfHours, taxes){
  return rate * numOfHours * (1 - taxes)
}

let profit = calculateProfit(hourlyRate, hours, taxRate)

console.log(profit)

Is it easy to add new features and functionality?


Slide 38 of 87
let hourlyRate = 250
let hours = 160
let taxRate = .35

function calculateProfit(rate, numOfHours, taxes){
  return rate * numOfHours * (1 - taxes)
}

function holdForTaxes(profitMade){
  return hourlyRate * hours - profitMade
}

let profit = calculateProfit(hourlyRate, hours, taxRate)

let taxesHeld = holdForTaxes(profit)

console.log(profit)

console.log(taxesHeld)

Slide 39 of 87

Can another developer look at my code and understand what is happening?


Slide 40 of 87
let hourlyRate = 250
let hours = 160
let taxRate = .35

function calculateProfit(rate, numOfHours, taxes){
  return rate * numOfHours * (1 - taxes)
}

function holdForTaxes(profitMade){
  return hourlyRate * hours - profitMade
}

let profit = calculateProfit(hourlyRate, hours, taxRate)

let taxesHeld = holdForTaxes(profit)

console.log(profit)

console.log(taxesHeld)

Slide 41 of 87

Slide 42 of 87
let hourlyRate = 250
let hours = 160
let taxRate = .35

function calculateProfit(rate, numOfHours, taxes){
  return rate * numOfHours * (1 - taxes)
}

let profit = calculateProfit(hourlyRate, hours, taxRate)

console.log(profit)

Can I make changes without losing sleep at night? 


Slide 43 of 87
let hourlyRate = 250
let hours = 160
let taxRate = .35

function calculateProfit(rate, numOfHours, taxes){
  return rate * numOfHours * (1 - taxes)
}

function holdForTaxes(profitMade){
  return hourlyRate * hours - profitMade
}

let profit = calculateProfit(hourlyRate, hours, taxRate)

let taxesHeld = holdForTaxes(profit)

console.log(profit)

console.log(taxesHeld)

Slide 44 of 87

DATA & FUNCTIONALITY


Slide 45 of 87
let hourlyRate = 250
let hours = 160
let taxRate = .35

DATA


Slide 46 of 87

Functionality

function calculateProfit(rate, numOfHours, taxes){
  return rate * numOfHours * (1 - taxes)
}

Slide 47 of 87

Slide 48 of 87

let seriousBusinessPerson = {
  hourlyRate: 250,
  hours: 160,
  taxRate: .35,
  calculateProfit: function() {
    return this.hourlyRate * this.hours * (1 - this.taxRate)
  }
}

Slide 49 of 87

Is it easy to add new features and functionality?


let seriousBusinessPerson = {
  hourlyRate: 250,
  hours: 160,
  taxRate: .35,
  calculateProfit: function() {
    return this.hourlyRate * this.hours * (1 - this.taxRate)
  }
}

Slide 50 of 87

let seriousBusinessPerson = {
  hourlyRate: 250,
  hours: 160,
  taxRate: .35,
  calculateProfit: function() {
    return this.hourlyRate * this.hours * (1 - this.taxRate)
  }
  calculateTaxesHeld: function(){
    return this.hourlyRate * this.hours - this.calculateProfit()
  }
}

Slide 51 of 87

Can another developer look at my code and understand what is happening?


let seriousBusinessPerson = {
  hourlyRate: 250,
  hours: 160,
  taxRate: .35,
  calculateProfit: function() {
    return this.hourlyRate * this.hours * (1 - this.taxRate)
  }
}

Slide 52 of 87

Can I make changes without losing sleep at night? 


let seriousBusinessPerson = {
  hourlyRate: 250,
  hours: 160,
  taxRate: .40, //changed
  calculateProfit: function() {
    return this.hourlyRate * this.hours * (1 - this.taxRate)
  }
}

Slide 53 of 87

This fusion of data and functionality into one object

Music & Light Warning - Next Slide


Slide 54 of 87

Encapsulation Baby


Slide 55 of 87

Slide 56 of 87

Encapsulation

The process of storing functions (methods) with their associated data (properties) - in one thing (object)*

*Nerds shaking violently


Slide 57 of 87

Is it easy to add new features and functionality?


Slide 58 of 87

Slide 59 of 87

Heats water for espresso and steam for your milk


Slide 60 of 87

Steam wand sucks...

What should the engineers do?


Slide 61 of 87

Do they get rid of the water boiler?

probably not...


Slide 62 of 87

At first, do they even need to think about the water boiler?


Slide 63 of 87

So the water boiling is abstracted?


Slide 64 of 87

Let's look at some code


Slide 65 of 87

function AgencyContractor(hourlyRate, hours, taxRate){
  this.hourlyRate = hourlyRate
  this.hours = hours
  this.taxRate = taxRate
  this.calculateProfit = function() {
    return this.hourlyRate * this.hours * (1 - this.taxRate)
  }
  this.invoiceClient = function(){
    return `Your invoice total is ${this.hourlyRate * this.hours}`
  }
}

let leon = new AgencyContractor(250,160,.35)
console.log( leon.invoiceClient() ) //40000
console.log( leon.hourlyRate ) //250
console.log( leon.calculateProfit() ) //26000

Ut Oh...

I PUT THIS CALCULATOR ON OUR AGENCY WEBSITE...


Slide 66 of 87
function AgencyContractor(hourlyRate, hours, taxRate){
  this.hours = hours
  this.taxRate = taxRate
  let rate = hourlyRate
  let calculateProfit = function() {
    return rate * this.hours * (1 - this.taxRate)
  }
  this.invoiceClient = function(){
    return `Your invoice total is ${rate * this.hours}`
  }
}

let leon = new AgencyContractor(250,160,.35)
console.log( leon.invoiceClient() ) //40000
console.log( leon.hourlyRate ) //undefined
console.log( leon.calculateProfit() ) 
//Uncaught TypeError: leon.calculateProfit is not a function

Better...

*Nerds shaking violently


Slide 67 of 87

So now my client can still get their invoice, but not see my hourly rate


Slide 68 of 87

Water boiling was hidden from the steam wand

Hourly rate was hidden from the client


Slide 69 of 87

Complex or unnecessary details are hidden


Slide 70 of 87

This enables you to implement things without understanding or even thinking about all the hidden complexity


Slide 71 of 87

Smaller more manageable pieces of code


Slide 72 of 87

Do stuff once

Music & Light Warning - Next Slide


Slide 73 of 87

Abstraction Baby


Slide 74 of 87

Slide 75 of 87

Abstraction

Hide details

and show essentials

SIMPLE, PREDICTABLE, MANAGEABLE


Slide 76 of 87

The Four Pillars

What if they actually made any sense...


Slide 77 of 87

Encapsulation

The process of storing functions (methods) with their associated data (properties) in one thing (object)


Slide 78 of 87

BUT WHY?


Slide 79 of 87

Made it easier to add new stuff

 

Made it easier to read through what was already coded

 

And made it so you were not afraid to make changes


Slide 80 of 87

Abstraction

Hide details

and show essentials


Slide 81 of 87

BUT WHY?


Slide 82 of 87

Smaller more manageable pieces of code

Helps you to split the complexity your software project into manageable parts


Slide 83 of 87

Inheritance

Polymophism

Next Class


Slide 84 of 87

Group Work

https://live.remo.co/e/100devs-objects-project


Slide 85 of 87

Right Of Passage


Slide 86 of 87

Get It To Work 

 

Then:
 

Make it easier to add new stuff

 

Make it easier to read through what was already coded

 

And make it so you are not afraid to make changes


Slide 87 of 87

Homework

 

Watch / Do: https://youtu.be/PFmuCDHHpwk

Read / Do: JS Way Ch. 09

Push? Read / Do: Eloquent JS Ch. 06

Do: 7 Codewars