Test
The code...
/* Namespace to put Assassins Brotherhood in */
var AssassinsBrotherhood = {};
/* functions for Assassins Brotherhood */
AssassinsBrotherhood.AddNinja = function(ninja) {
AssassinsBrotherhood.Ninjas.push(ninja);
};
/* properties for Assassins Brotherhood */
AssassinsBrotherhood.Ninjas = new Array();
// #########################################################
/* available weapons; Katana */
function Katana() {
// properties of the instance
this.isSharp = true;
};
Katana.prototype.use = function() {
// weapon is unsharp after usage
this.isSharp = false;
};
// #########################################################
/* create the ninja as instanceable */
function Ninja(name) {
// save name to instance
this.name = name;
// default properties
this.weapons = new Array();
};
/* functions of the Ninja */
Ninja.prototype.train = function(weapon) {
this.weapons.push(weapon);
};
// #########################################################
/* add a ninja, weapon and then use weapon */
var ninja1 = new Ninja('Asakura Yoshikage');
AssassinsBrotherhood.AddNinja(ninja1);
document.write('Assassins Brotherhood added the ninja '+AssassinsBrotherhood.Ninjas[0].name+'<br/>');
// add second ninja
var ninja2 = new Ninja('Nikaido Moriyoshi ');
AssassinsBrotherhood.AddNinja(ninja2);
document.write('Assassins Brotherhood added the ninja '+AssassinsBrotherhood.Ninjas[1].name+'<br/>');
// how many ninjas?
document.write('Assassins Brotherhood has '+AssassinsBrotherhood.Ninjas.length+' Ninjas in storage<br/>');
// any weapon for ninja 1?
if ( AssassinsBrotherhood.Ninjas[0].weapons.length == 0 ) {
// add katana to first ninja
var katana1 = new Katana();
AssassinsBrotherhood.Ninjas[0].train(katana1);
// check weapon
if ( AssassinsBrotherhood.Ninjas[0].weapons[0] instanceof Katana ) {
document.write('The Ninja '+AssassinsBrotherhood.Ninjas[0].name+' trained Katana.<br/>');
} else {
document.write('The Ninja '+AssassinsBrotherhood.Ninjas[0].name +' has an unkown weapon.<br/>');
}
} else {
document.write('The Ninja '+AssassinsBrotherhood.Ninjas[0].name+' has '+AssassinsBrotherhood.Ninjas[0].weapons.length+' weapons trained.<br/>');
}
// check status of first weapon
document.write(AssassinsBrotherhood.Ninjas[0].name+' has first weapon sharpend: '+AssassinsBrotherhood.Ninjas[0].weapons[0].isSharp+'.<br>');
// use weapon and write status
AssassinsBrotherhood.Ninjas[0].weapons[0].use();
document.write(AssassinsBrotherhood.Ninjas[0].name+' uses his first weapon and notices sharpen: '+AssassinsBrotherhood.Ninjas[0].weapons[0].isSharp+'.<br>');