fork download
  1. function Chr(type, name) {
  2. this._type = type;
  3. this._name = name;
  4.  
  5. // Object.getPrototypeOf( this ) == this.__proto__
  6. Object.getPrototypeOf( this ).type = function() { return this._type; };
  7. Object.getPrototypeOf( this ).name = function() { return this._name; };
  8.  
  9. Object.getPrototypeOf( this ).str = function() {
  10. return "Soy un " + this.type() + " y me llamo " + this.name();
  11. }
  12. }
  13.  
  14. function Warrior(type, name) {
  15. this._type = type;
  16. this._name = name;
  17.  
  18. Object.getPrototypeOf( this ).str = function() {
  19. return "Soy un guerrero " + this.type() + ". ¡Soy " + this.name() + "!";
  20. }
  21.  
  22. Object.getPrototypeOf( this ).attak = function(x) {
  23. if ( !( x instanceof Chr ) ) {
  24. return "¡No puedes atacar a eso!";
  25. }
  26.  
  27. return x.name() + "!! Raarrrrr!!";
  28. }
  29. }
  30. Warrior.prototype.__proto__ = Chr.prototype;
  31.  
  32. v1 = new Chr( "Vegano", "Gaggen" );
  33. g1 = new Warrior( "Kyndr", "Rork" );
  34.  
  35. let txt = v1.str();
  36. txt += "\n" + g1.str();
  37. txt += "\n\n" + g1.attak( "Gaggen" );
  38. txt += "\n" + g1.attak( v1 );
  39. txt += "\n\nEl prototipo padre del guerrero es el del vegano: "
  40. + ( ( g1.__proto__.__proto__ == v1.__proto__ )? "sí" : "no" );
  41. txt += "\n\n" + g1.name() + " instanceof Chr: "
  42. + ( ( g1 instanceof Chr )? "sí" : "no" );
  43. txt += "\n\n" + g1.name() + " instanceof Warrior: "
  44. + ( ( g1 instanceof Warrior)? "sí" : "no" );
  45.  
  46. txt += "\n\n" + v1.name() + " instanceof Chr: "
  47. + ( ( v1 instanceof Chr )? "sí" : "no" );
  48. txt += "\n\n" + v1.name() + " instanceof Warrior: "
  49. + ( ( v1 instanceof Warrior)? "sí" : "no" );
  50.  
  51. print( txt );
  52.  
Success #stdin #stdout 0.02s 16572KB
stdin
Standard input is empty
stdout
Soy un Vegano y me llamo Gaggen
Soy un guerrero Kyndr. ¡Soy Rork!

¡No puedes atacar a eso!
Gaggen!! Raarrrrr!!

El prototipo padre del guerrero es el del vegano: sí

Rork instanceof Chr: sí

Rork instanceof Warrior: sí

Gaggen instanceof Chr: sí

Gaggen instanceof Warrior: no