WDD 131: Dynamic Web Fundamentals

W04 Learning Activity: JavaScript Objects

Overview

JavaScript objects are a way to group related data and functions together. Objects are a core concept in JavaScript and are used extensively in the language. In this activity, you will learn how to create and use objects in JavaScript.

"An object is a collection of related data and/or functionality. These usually consist of several variables and functions (which are called properties and methods when they are inside objects)." – MDN

Course Learning Outcomes
  1. Demonstrate proficiency with JavaScript language syntax.
  2. Use JavaScript to respond to events and dynamically modify HTML.

Prepare

Instead of storing related data in complex parallel or multi-dimensional arrays, related data can be stored using objects which are more readable, stable, and maintainable.

This is an example of a JavaScript object literal (a collection of key-value pairs) representing a person in a concise and readable format.

let person = {
  name: "Antonia Francesca",
  age: 30,
  profession: "Software Engineer",
  hobbies: ["reading", "playing guitar", "hiking"],
  address: {
    street: "123 Camino Real",
    city: "Santa Rosa",
    country: "Honduras"
  },
  isEmployed: true,
  greet: function() {
    console.log(`Hello, my name is ${this.name}.`);
  }
};

✓ Determine the output of the following statements given the person object defined above.

  1. console.log(person['isEmployed']);   // bracket notation
  2. console.log(person.hobbies[0]);   // dot notation
  3. console.log(person.age);   // dot notation
  4. console.log(person['address'].city);   // combined notation
Answers 1. true
2. reading (gets the first hobby in the list)
3. 30
3. Santa Rosa

Updating a value works like you expect.

person.age = 31;

Reference: Object Basics – MDN

Activity Instructions

Use objects and encapsulation to create a representation of a typical college course schedule.

The following CodePen ☼ JavaScript Objects can be used to work on this activity.

  1. Given the following object literal for a course,
    let aCourse = {
      code: "WDD131",
      title: "Dynamic Web Fundamentals",
      credits: 2,
    };
    add a sections property to the object. Since the course may have more than one section, make this sections an array of at least two (2) items with the following properties:
    • section
    • enrolled
    • instructor
    Check Your Understanding
    let aCourse = {
      code: "WDD131",
      title: "Dynamic Web Fundamentals",
      credits: 2,
      sections : [
        { section: "001", enrolled: 95, instructor: "Roberto Diaz Rodriguez" },
        { section: "002", enrolled: 80, instructor: "Sarah Gobble" }
      ]
    };
  2. Create a function to set the name and number of the course in HTML. Pass the course object as a parameter into your function. Use dot notation to access the parts of the object that you need to build the output in the element with an id of courseName.
    Check Your Understanding
    function setCourseInformation(course) {
      document.querySelector("#courseName").innerHTML = `${course.code} – ${course.title}`;
    }
  3. Create another function that will output the sections into a table with an id of sections as individual rows of data.
    Check Your Understanding
    function renderSections(course) {
      const tbody = document.querySelector("#sections tbody");
      let rows = "";
      for (const section of course.sections) {
        rows += `<tr>
          <td>${section.section}</td>
          <td>${section.enrolled}</td>
          <td>${section.instructor}</td>
        </tr>`;
      }
      tbody.innerHTML = rows;
    }

Example Solution: CodePen ☼ JavaScript Objects