Get Programming with JavaScript - Listing 4.01
Listing 4.01 - Displaying an object's properties on the console
JS Bin
var movie1;
movie1 = {
title: "Inside Out",
actors: "Amy Poehler, Bill Hader",
directors: "Pete Doctor, Ronaldo Del Carmen"
};
console.log("Movie information for " + movie1.title);
console.log("------------------------------");
console.log("Actors: " + movie1.actors);
console.log("Directors: " + movie1.directors);
console.log("------------------------------");
Further Adventures
Listing 4.01 - Displaying an object's properties on the console - Task 1
- Add a second movie and display the same info for it.
var movie1;
var movie2; // declare a second variable
movie1 = {
title: "Inside Out",
actors: "Amy Poehler, Bill Hader",
directors: "Pete Doctor, Ronaldo Del Carmen"
};
// Create a second movie object
// Assign it to the second variable
movie2 = {
title: "Tomorrowland",
actors: "George Clooney, Britt Robertson",
directors: "Brad Bird"
};
console.log("Movie information for " + movie1.title);
console.log("------------------------------");
console.log("Actors: " + movie1.actors);
console.log("Directors: " + movie1.directors);
console.log("------------------------------");
// Display the second movie
console.log("Movie information for " + movie2.title);
console.log("------------------------------");
console.log("Actors: " + movie2.actors);
console.log("Directors: " + movie2.directors);
console.log("------------------------------");
Listing 4.01 - Displaying an object's properties on the console - Task 2
- Create an object to represent a blog post.
var movie1;
var movie2;
var blogPost; // declare a variable
movie1 = {
title: "Inside Out",
actors: "Amy Poehler, Bill Hader",
directors: "Pete Doctor, Ronaldo Del Carmen"
};
movie2 = {
title: "Tomorrowland",
actors: "George Clooney, Britt Robertson",
directors: "Brad Bird"
};
// Create a blog post object
// Assign it to the variable
blogPost = {
title: "My Adventure Begins",
author: "BB",
post: "I've been invited on an adventure by a bunch of dwarves and a wizard!"
};
console.log("Movie information for " + movie1.title);
console.log("------------------------------");
console.log("Actors: " + movie1.actors);
console.log("Directors: " + movie1.directors);
console.log("------------------------------");
console.log("Movie information for " + movie2.title);
console.log("------------------------------");
console.log("Actors: " + movie2.actors);
console.log("Directors: " + movie2.directors);
console.log("------------------------------");
Listing 4.01 - Displaying an object's properties on the console - Task 3
- Write code to display info about the blog post.
var movie1;
var movie2;
var blogPost;
movie1 = {
title: "Inside Out",
actors: "Amy Poehler, Bill Hader",
directors: "Pete Doctor, Ronaldo Del Carmen"
};
movie2 = {
title: "Tomorrowland",
actors: "George Clooney, Britt Robertson",
directors: "Brad Bird"
};
blogPost = {
title: "My Adventure Begins",
author: "BB",
post: "I've been invited on an adventure by a bunch of dwarves and a wizard!"
};
console.log("Movie information for " + movie1.title);
console.log("------------------------------");
console.log("Actors: " + movie1.actors);
console.log("Directors: " + movie1.directors);
console.log("------------------------------");
console.log("Movie information for " + movie2.title);
console.log("------------------------------");
console.log("Actors: " + movie2.actors);
console.log("Directors: " + movie2.directors);
console.log("------------------------------");
// Display the blog post
console.log(blogPost.title);
console.log("by " + blogPost.author);
console.log("------------------------------");
console.log(blogPost.post);
console.log("------------------------------");