Listing 9.04 - A Planet constructor
JS Bin
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();
Listing 9.04 - A Planet constructor - Tasks 1&2
- Use the Planet constructor function to create a second planet. Don't forget the 'new' keyword.
- Call the showPlanet method on your newly created planet.
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();