Day 3: DOM, Arrays and Objects

Wednesday, June 27, 2018

Lecture Videos

Morning:

Afternoon:

Topics

  • Creating and appending DOM Elements
  • Flexbox and CSS
  • Arrays and Objects
  • ES2015+ (ES6) - Babel docs
  • this

Flexbox

CSS

  • CSS Tricks: A great place to search for tips and tricks in css

Examples

DOM

If we start with the following markup:



<div id="my-div"></div>


We can add additional markup to it programmatically using JavaScript. One way is to create new HTML elements using document.createElement, and adding them by using appendChild. Styling of the element can even be changed by manipulating the element’s style property.



// create an h1 and modify text content and color
const heading = document.createElement('h1')
heading.textContent = "This is the best heading I've ever seen"
heading.style.color = "red"

// get a reference to the existing div and add the heading as a child element
const div = document.querySelector('#my-div')
div.appendChild(heading)


This will produce the following markup:



<div id="my-div">
  <h1 style="color: red;">This is the best heading I've ever seen</h1>
</div>


Arrays

Arrays are extremely useful data structures in JavaScript, as they can be easily iterated and transformed through methods like map, filter, and reduce.



let arrayOfFood = ['pasta', 'chicken parmigiana', 'spaghetti', 'meatballs']

const result = arrayOfFood.filter(food => food.length > 10)  // ['chicken parmigiana']



Objects

Almost everything in JavaScript is an Object. The easiest way to create new Objects is with the object initializer, more commonly known as ‘object literal’ syntax. Basically, use curly braces to make an object {} and fill in the properties that you want.

Objects contain key/value pairs that allow you to set and retrieve values from them.



// create a new object and assign some properties
const myObject = {
  prop1: 'Hello there',
  prop2: 42
}

// access the values in several ways, usually 'dot' or 'square bracket' notation
myObject.prop1 // => 'Hello there'
myObject['prop1'] //=> 'Hello there'

// new key/value pairs can also be assigned with these notations
myObject.prop3 = 'New Value!'
myObject['prop4'] = 'Newest Value!'

console.log(myObject)
// { 
//   prop1: 'Hello there',
//   prop2: 42,
//   prop3: 'New Value!',
//   prop4: 'Newest Value!'
// }


We know that we can iterate through an Array using map or forEach. Can we do something similar with objects? There are a few ways to do it, but one of the newest and easiest is the Object.keys method. It iterates through the enumerable properties of an object, returning an array of the property keys. Once we have an array of keys, we can map over that and access each of the object properties individually.



const myObj = {
  a: 'hi',
  b: 42,
  c: [1, 2, 3]
}

const myObjKeys = Object.keys(myObj)    // ['a', 'b', 'c']

myObjKeys.map(key => myObj[key])        // ['hi', 42, [1, 2, 3]]


ES2015+ (ES6+)

  • Inheritance (with the ES2015 class syntax; it’s still prototypal inheritance though) - ES6 Classes

this

The same function can have different values for this depending on how the function is called/invoked.



const app = {
  sayYeah() {
    console.log(`Yeah, ${this}`)
  },
  
  toString() {
    return 'app object'
  }
}

// When invoked as a method
app.sayYeah() // "Yeah, app object"

// When invoked as an event handler
document
  .querySelector('button')
  .addEventListener('click', app.sayYeah)
  // "Yeah, [object HTMLButtonElement]"

// When manually set with bind
app.sayYeah.bind('w00t')() // "Yeah, w00t"


Presentations

Projects

CodePen

Morning | Afternoon

Chrismess

Morning | Afternoon

Homework

  • In addition to building a list item and adding it to the DOM (as we are now), also store each flick in an array.

Bonus Credit

  • Add a delete button to each list item that removes it from the list.

Super Mega Bonus Credit

  • Remove the flick from the array as well.

Super Mega Bonus Credit Hyper Fighting

  • Add a favorite button to each flick as well that lets you mark your favorites.
  • Display favorites differently.
  • Be sure the favorites are recorded appropriately in the array.