Get Programming with JavaScript - Listing 9.04

Listing 9.04 - A Planet constructor

var Planet = function (name, position, type) { this.name = name; this.position = position; this.type = type; this.showPlanet = function () { var info = this.name + ": planet " + this.position; info += " - " + this.type; console.log(info); }; }; var planet = new Planet( "Jupiter", 5, "Gas Giant" ); planet.showPlanet();

Further Adventures

Listing 9.04 - A Planet constructor - Tasks 1&2

var Planet = function (name, position, type) { this.name = name; this.position = position; this.type = type; this.showPlanet = function () { var info = this.name + ": planet " + this.position; info += " - " + this.type; console.log(info); }; }; var planet = new Planet( "Jupiter", 5, "Gas Giant" ); // create a second planet var planet2 = new Planet( "Neptune", 8, "Ice Giant" ); planet.showPlanet(); // display the planet planet2.showPlanet();