Get Programming with JavaScript - Listing 4.10
Listing 4.10 - Calling the showMovieInfo function
var movie1;
var showMovieInfo;
var movie;
movie1 = {
title: "Inside Out",
actors: "Amy Poehler, Bill Hader",
directors: "Pete Doctor, Ronaldo Del Carmen"
};
showMovieInfo = function () {
console.log("Movie information for " + movie.title);
console.log("------------------------------");
console.log("Actors: " + movie.actors);
console.log("Directors: " + movie.directors);
console.log("------------------------------");
};
movie = movie1;
showMovieInfo();
Further Adventures
Listing 4.10 - Calling the showMovieInfo function - Task 1
- Without declaring a movie2 variable, assign movie2 to the movie variable instead of movie1.
var movie1;
var showMovieInfo;
var movie;
movie1 = {
title: "Inside Out",
actors: "Amy Poehler, Bill Hader",
directors: "Pete Doctor, Ronaldo Del Carmen"
};
showMovieInfo = function () {
console.log("Movie information for " + movie.title);
console.log("------------------------------");
console.log("Actors: " + movie.actors);
console.log("Directors: " + movie.directors);
console.log("------------------------------");
};
movie = movie2; // Assign movie2 to movie
showMovieInfo();
The movie2 variable has not been declared. An error will occur.
Listing 4.10 - Calling the showMovieInfo function - Task 2
- Create an empty object and assign it to a movie2 variable.
var movie1;
var movie2; // Declare a variable
var showMovieInfo;
var movie;
movie1 = {
title: "Inside Out",
actors: "Amy Poehler, Bill Hader",
directors: "Pete Doctor, Ronaldo Del Carmen"
};
// Create an empty object
// Assign it to movie2
movie2 = {};
showMovieInfo = function () {
console.log("Movie information for " + movie.title);
console.log("------------------------------");
console.log("Actors: " + movie.actors);
console.log("Directors: " + movie.directors);
console.log("------------------------------");
};
movie = movie2;
showMovieInfo();
The movie2 variable has been declared and assigned an empty object as a value. The title, actors and directors properties are undefined.
Listing 4.10 - Calling the showMovieInfo function - Task 3
- Fill out movie2 with the properties needed by showMovieInfo.
var movie1;
var movie2;
var showMovieInfo;
var movie;
movie1 = {
title: "Inside Out",
actors: "Amy Poehler, Bill Hader",
directors: "Pete Doctor, Ronaldo Del Carmen"
};
// Assign needed properties
movie2 = {
title: "Tomorrowland",
actors: "Britt Robertson, George Clooney",
directors: "Brad Bird"
};
showMovieInfo = function () {
console.log("Movie information for " + movie.title);
console.log("------------------------------");
console.log("Actors: " + movie.actors);
console.log("Directors: " + movie.directors);
console.log("------------------------------");
};
movie = movie2;
showMovieInfo();
The movie2 variable has been declared and assigned an object as a value. The title, actors and directors properties have been set and will be displayed.