Test
The code...
// baseclass for Person - no constructor
function Person() { };
// getter for name
Person.prototype.getName = function() {
return this.name;
};
// setter for name
Person.prototype.setName = function(name) {
this.name = name;
};
// create class for Ninja that should have a name
function Ninja(name) {
this.setName(name);
};
// create inheritence to Person
Ninja.prototype = new Person();
// continue to add functions for Ninja in the prototype chain
// which is now an instance of Person
Ninja.prototype.trainWeapon = function(weapon) {
if ( this.weapons == undefined) {
this.weapons = new Array();
}
this.weapons.push(weapon);
};
Ninja.prototype.getWeapon = function(num) {
return this.weapons[num];
};
Ninja.prototype.getWeaponNumber = function() {
if ( this.weapons == undefined) {
return 0;
} else {
return this.weapons.length;
}
};
// #######################################
// create a new ninja
var ninja1 = new Ninja('Akiba Hosamurro');
// output name and num of weapons
document.write('The name of the ninja is: '+ ninja1.getName()+'<br>');
document.write('The ninja has ' + ninja1.getWeaponNumber() + ' weapons trained<br>');
// train a weapon and output first weapon
ninja1.trainWeapon('Bokken');
document.write(ninja1.getName() + ' has ' + ninja1.getWeapon(0) + ' as first weapon');