JS-101-Fundamentals
  • JavaScript Library
  • 1-JavaScript Fundamentals
    • 0-Getting Started
      • Welcome
    • 1-JS-Fundamentals
      • 1-Grammar and Types
        • Comments
        • Declarations
        • Scope
        • Hoisting
        • Types
        • Literals
      • 2-Control Flow and Error Handling
        • if
        • if else
        • switch
        • try catch
        • throw
      • 3-Loops
        • For Loops
        • For In Loops
        • For Of Loops
        • Do While
        • While Loops
      • 4-Functions
        • Declarations
        • Expressions
        • Calling Functions
        • Scope
        • Parameters
        • Arguments
        • Closures
        • Closures Challenge
        • Arrow Functions
        • Predefined
      • 5-Expressions and Operators
        • Assignment
        • Comparison
        • Ternary
        • Typeof
        • Left Hand Side
        • Spread
      • 6 Numbers and Dates
        • Numbers
          • Numbers Enhanced
        • Math
        • Dates
      • 7 String Methods
        • String Literals
        • Methods
      • 8 Regular Expressions
        • Basic Intro
      • 9 Arrays
        • Array Review
        • Populating/Referring
        • Length
        • Iterating
        • Methods
      • 10 Objects
        • About Objects
        • Properties
        • Enumeration
        • Initializers
        • Constructor Functions
        • this
        • create
        • Methods
      • 11 ES6 Intro
        • ES6 Intro
        • let
        • const
Powered by GitBook
On this page
  • File Location
  • An object with no properties
  • Object Literal with two properties
  • Nested properties
  • Challenges
  1. 1-JavaScript Fundamentals
  2. 1-JS-Fundamentals
  3. 10 Objects

Properties

File Location

We will be working in the following file:

    javascript-library
        └── 0-PreWork
        └── 1-Fundamentals
            └── 10-Objects
                01-properties.js <----You will be working in this file.

In the pre-work, you learned about how to create an object and assign it properties. This is just a review.

An object with no properties

var empty_object = {};

Object Literal with two properties

Look at the code below.

var movie = {
    //These are properties
    name : "The Godfather",
    director: "Martin Scorsese"
};

Use . to access the properties.

console.log(movie.name); 
console.log(movie["name"]);//No one does this, but you can.
//Quotes are optional around property names
var band = {
    "name" : "Weird Al Yankovic",
    "numberAlbums": 1,
};

console.log(band.name);
console.log(band.numberAlbums);

Nested properties

You also saw in the left had side module how objects can be nested. Arrays can also nested inside objects.

var netflix = {
    id: 1,
    name: "",
    seasonInfo: {
        episodeInfo : [
            { episode: 1, episodeName: "Hey Paul"},
            { episode: 2, episodeName: "Yes Paul"}
        ]
    }
};

console.log("All data:", netflix);
console.log("Just season info: ", netflix.seasonInfo);
console.log("Episode Number: ", netflix.seasonInfo.episodeInfo[0].episode);

We could loop over the episode info array like this

for (var container in netflix.seasonInfo.episodeInfo.episodeName){
    console.log(netflix.seasonInfo.episodeInfo[container].episodeName);
}

Challenges

  1. Create an object with several properties. Print each to the console.

  2. Add an array as a property and loop over its elements. Print them to the console.

  3. Recall that objects can be created from a model. Create a model asking for a person's age, height, eye color, hair color, and whether or not they wear glasses (make this a boolean value). Then create yourself as an object with that model.

PreviousAbout ObjectsNextEnumeration

Last updated 6 years ago