Listing 9.01 - Using a function to create an object
JS Bin
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);
Listing 9.01 - Using a function to create an object - Tasks 1&2
- Build a second planet, using the buildPlanet function.
- Log its name and type.
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);