JavaScript 6 - Object Literals

Projected Time

About 60 minutes

Prerequisites

Motivation

What companies use object literals? Many companies need to enclose data into reusable packages. An example would be Facebook, making an object to contain the information for a single user.

The object for a Facebook user may look something like this:

const person = {
  name: ['Sara', 'Crosby'],
  age: 35,
  gender: 'female',
  favoriteComment: function() {
    alert('Good morning!');
  };
}

For a more complex example, directly from Facebook, check here: https://developers.facebook.com/docs/graph-api/reference/v5.0/object/likes

Objectives

Participants will be able to:

Specific Things to Learn

Materials

Lesson

JavaScript VI - Object Literals (slides) | JS 6 Walkthrough Video of Slides

What is an Object?

An object is a thing that has properties. This sounds simple, but it’s actually very abstract! To help flesh this out, think of an example software application that keeps track of books, such as for a library. In this application, a book can be thought of as an object that has certain properties like title and author.

For example:

let book = {
  "id": "827392838",
  "authorFirstName": "Jane",
  "authorLastName": "Doe",
  "title": "The Wonderful World of JavaScript",
};

In the same example software application, we might also want to keep track of people who will borrow library books:

let borrower = {
  "id": "9002",
  "firstName": "Syma",
  "middleInitial": "N",
  "lastName": "Tec",
  "phoneNumber": "(415) 123-1234",
};

To tie together a book and the borrower, we might want to have yet another object that represents the book loan:

let loan = {
  "bookId": "827392838",
  "borrowerId": "9002",
  "borrowedDate": "2018-08-26",
  "dueDate": "2018-09-26",
  "timesRenewed": 0,
}

Things to Remember

Independent Practice

Supplemental Materials

Check for Understanding