Get Programming with JavaScript - Listing 7.01

Listing 7.01 - Passing a function an object as an argument

var planet1; var getPlanetInfo; planet1 = { name: "Jupiter", position: 5, type: "Gas Giant", radius: 69911, sizeRank: 1 }; getPlanetInfo = function (planet) { return planet.name + ": planet number " + planet.position; }; console.log(getPlanetInfo(planet1));

Further Adventures

Listing 7.01 - Passing a function an object as an argument - Tasks 1&2

var planet1; var planet2; // declare a variable var getPlanetInfo; planet1 = { name: "Jupiter", position: 5, type: "Gas Giant", radius: 69911, sizeRank: 1 }; // create a second object and // assign it to your variable planet2 = { name: "Neptune", position: 8, type: "Ice Giant", radius: 24622, sizeRank: 4 }; getPlanetInfo = function (planet) { return planet.name + ": planet number " + planet.position; }; console.log(getPlanetInfo(planet1)); console.log(getPlanetInfo(planet2)); // log planet2

Listing 7.01 - Passing a function an object as an argument - Task 3

var planet1; var planet2; var getPlanetInfo; planet1 = { name: "Jupiter", position: 5, type: "Gas Giant", radius: 69911, sizeRank: 1 }; planet2 = { name: "Neptune", position: 8, type: "Ice Giant", radius: 24622, sizeRank: 4 }; // include extra info getPlanetInfo = function (planet) { var info = planet.name + ": planet number " + planet.position; info += "\n" + planet.type + " of radius " + planet.radius + "km"; info += " (" + planet.sizeRank + ")"; return info; }; console.log(getPlanetInfo(planet1)); console.log(getPlanetInfo(planet2));