Skip to content

Instantly share code, notes, and snippets.

@jnewman12
Created December 1, 2016 06:39
Show Gist options
  • Save jnewman12/c7b7bb5e376394591c64f0cf34c26fc9 to your computer and use it in GitHub Desktop.
Save jnewman12/c7b7bb5e376394591c64f0cf34c26fc9 to your computer and use it in GitHub Desktop.
JS Objects Lab

JS Objects Lab


Building and Referencing Objects

  • Lets's take what we've learned about JS objects and put it to work!

Model Yourself from Your Parents

  • create yourself as an object
  • However, build your parents first!

  1. First build two objects mom and dad as object literals (e.g. var mom = {}). If you have a non-traditional family structure, just use someone everybody loves, like Joe Biden or Oprah.
  2. Create one Physicality object and one Personality object for each parent (e.g. mom.physicality = {} and mom.personality = {}).
  3. Using different data types (Number, String, Array, Object, Null, & Boolean), add 5 physical attributes to each of your parents.
  4. Do the same with the personality object!
  5. Now build yourself by referencing physical and personality attributes from your folks!
  6. Make 2 lists: first, what you're good at, and second, what you're bad at.
  7. Age yourself.
  8. Delete half of the things you were good at.
  9. Age yourself (again).
  10. Add 'sitting' to the things you're good at and remove everything else from the list.
  11. Add 5 more things to the list of things you're bad at.

BONUS: Create a method that prints out one of your classic sayings!


More Practice!

  • If you finish the lab, try one of these exercises! There's no starter code for these, so you can start from scratch

  • You are now a pokemon trainer. Yes, your middle school dreams (elementary school, maybe? okay, high school.) are now fulfilled.
  • Build Your starting six pokemon!
  1. Create each pokemon as an object literal.
  2. Give each pokemon at least eight properties - be sure to use each data type in the properties (think health: 500 or moves: ['ice punch', 'friend chip', 'mega sneeze']).
  3. Give each pokemon a reference to another pokemon, in case they faint!
  4. Create an array myPokemon and push or unshift each pokemon into the array in the order you choose.
  5. BONUS: Give your pokemon a move as a method - make it remove an amount of HP from another pokemon foe!!

Build a Pizza Menu

  • Become the sweaty human behind the counter, make your menu, and start making some friggin' pizzas.
  1. Create a menu object.
  2. Create 5 pizzas, each referenced within the menu object as distinct properties (like dailySpecial: { fill this in on the next step }).
  3. Give each pizza 6 properties.
  4. Be sure to use each data type in the properties (think calories: 5000, meat: null or preferredBy: 'vegetarians').
  5. BONUS: Create a method on the menu object that prints to the console the entirety of the menu!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment