Objects and PEDAC

Current Data Structures

// simple variable
const sides = 6;

// array
const band = ["drum kit", "bass", "guitar", "trumpet", "trombone", "tenor"]

 

Objects allow us to store semantically related data together in one place

Without Objects

const playerName = "Barron"

const playerInstrument = "trombone"

const playerPay = 40000

Objects

const trombonist = {
    firstName:"Barron", 
    lastName:"Redheart",
    instrument: "trombone",
    age:38, 
    hasHair: false,
    pay: 40000,
    sayHello: () => {
        console.log("G'day Mate")
    },
    rockOut: () => {
        console.log("wah-wah-wah")
    }
}

// print out players instrument
console.log(trombonist.instrument)

Use Objects

// create an empty object
const empty = {}

// create an object with properties
const cardData = {
    value: 10,
    suit:"Diamonds",
    rank:"Ace"
}

// access properties

console.log(cardData.value)

Use Objects

const person = {
    name: "Barron Redheart"
}

// assign/update properties
person.name = "Kal Vilmer"

// what happens with this?
console.log(person.favoriteMovie)

Example

const band = []

const addMemberToBand = () => {
    const name = getNameFromDOM()
    const instrument = getInstrumentFromDOM()
    const pay = getPayFromDOM()

    const member = {
        instrument: instrument,
        name: name,
        pay: pay
    }
    band.push(member)
}

Our tool belt has many tools but how do I use them?

PEDAC

  • understanding the Problem
  • Example data to validate the problem
  • Data structures
  • create the Algorithm
  • Code with intent

Let's create a card shuffler that uses objects.

(hint: might be useful for this weekend's project)

Objects and PEDAC

By Mark Dewey

Objects and PEDAC

  • 232