Day 3: The DOM Cont'd / Arrays and Objects

Wednesday, June 6, 2018

Lecture Videos

Morning:

Afternoon:

Topics

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

Objects

  • Object literals
  • Property Naming
  • Retrieving property values
  • Setting property values

Styling

Flexbox

Tips and Tricks on CSS

This

  • default binding through function invocation
  • implicit binding through method calls
  • explicit binding with .bind

Examples

Arrays

Arrays are extremely useful data structures in JavaScript, as they can be easily iterated and transformed through methods like map, filter, and reduce. Sometimes, you may have an ‘array-like’ collection (like a NodeList or function arguments) that you would need to convert to an actual Array before you could use these methods. This can be done using Array.from



let paragraphs = document.querySelectorAll('p')
paragraphs.map((p) => {
  p.textContent = "This won't work because paragraphs is a NodeList, not Array!"
})
// => Uncaught TypeError: paragraphs.map is not a function

let actualArrayOfParagraphs = Array.from(paragraphs)
actualArrayOfParagraphs.map((p) => {
  p.textContent = "This totally does work because we created an Array from our NodeList!"
})


Requirements for 'Array.from'

What objects can you convert to an Array using ‘Array.from’?

  • Any array-like object with a ‘length’ property and indexed elements
  • Iterable objects (like Map or Set)

For more info, check out this article on MDN.

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]]


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

Spellbook

Morning | Afternoon

Homework

  • In addition to building a list item and adding it to the DOM (as we are now), also store each spell 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 item from the array as well.