function Chr(type, name) {
this._type = type;
this._name = name;
// Object.getPrototypeOf( this ) == this.__proto__
Object.getPrototypeOf( this ).type = function() { return this._type; };
Object.getPrototypeOf( this ).name = function() { return this._name; };
Object.getPrototypeOf( this ).str = function() {
return "Soy un " + this.type() + " y me llamo " + this.name();
}
}
function Warrior(type, name) {
this._type = type;
this._name = name;
Object.getPrototypeOf( this ).str = function() {
return "Soy un guerrero " + this.type() + ". ¡Soy " + this.name() + "!";
}
Object.getPrototypeOf( this ).attak = function(x) {
if ( !( x instanceof Chr ) ) {
return "¡No puedes atacar a eso!";
}
return x.name() + "!! Raarrrrr!!";
}
}
Warrior.prototype.__proto__ = Chr.prototype;
v1 = new Chr( "Vegano", "Gaggen" );
g1 = new Warrior( "Kyndr", "Rork" );
let txt = v1.str();
txt += "\n" + g1.str();
txt += "\n\n" + g1.attak( "Gaggen" );
txt += "\n" + g1.attak( v1 );
txt += "\n\nEl prototipo padre del guerrero es el del vegano: "
+ ( ( g1.__proto__.__proto__ == v1.__proto__ )? "sí" : "no" );
txt += "\n\n" + g1.name() + " instanceof Chr: "
+ ( ( g1 instanceof Chr )? "sí" : "no" );
txt += "\n\n" + g1.name() + " instanceof Warrior: "
+ ( ( g1 instanceof Warrior)? "sí" : "no" );
txt += "\n\n" + v1.name() + " instanceof Chr: "
+ ( ( v1 instanceof Chr )? "sí" : "no" );
txt += "\n\n" + v1.name() + " instanceof Warrior: "
+ ( ( v1 instanceof Warrior)? "sí" : "no" );
print( txt );