Get Programming with JavaScript - Listing 9.01

Listing 9.01 - Using a function to create an object

var buildPlanet = function (name, position, type) { var planet = {}; planet.name = name; planet.position = position; planet.type = type; return planet; }; var planet1 = buildPlanet( "Jupiter", 5, "Gas Giant" ); console.log(planet1.name); console.log(planet1.position); console.log(planet1.type);

Further Adventures

Listing 9.01 - Using a function to create an object - Tasks 1&2

var buildPlanet = function (name, position, type) { var planet = {}; planet.name = name; planet.position = position; planet.type = type; return planet; }; var planet1 = buildPlanet( "Jupiter", 5, "Gas Giant" ); // build a second planet var planet2 = buildPlanet( "Neptune", 8, "Ice Giant" ); console.log(planet1.name); console.log(planet1.position); console.log(planet1.type); // log its name and type console.log(planet2.name); console.log(planet2.type);