//-----------------------------------------------------------------------------------------------------------------

// properties are directly passed to `create` method
var Person = Class.create({
  initialize: function(name) {
    this.name = name;
  },
  say: function(message) {
    return this.name + ': ' + message;
  }
});

// when subclassing, specify the class you want to inherit from
var Pirate = Class.create(Person, {
  // redefine the speak method
  say: function($super, message) {
    return $super(message) + ', yarr!';
  }
});

var nestor = new Person("Nestor");
//alert(nestor.say('11 22 33'));

var john = new Person('David');
//alert(john.say('aa bb cc'));



//-----------------------------------------------------------------------------------------------------------------


//-----------------------------------------------------------------------------------------------------------------
var Logger = Class.create({
  initialize: function() {
    // this is the right way to do it:
    this.log = [];
  },
  write: function(message) {
    this.log.push(message);
  }
});


var logger = new Logger;
  logger.log; // -> []
  logger.write('A'); // -> [A]
  logger.write('B'); // -> [A,B]
  logger.write('C'); // -> [A,B,C]
  logger.write('D'); // -> [A,B,C,D]
  logger.write('F'); // -> [A,B,C,D,E]
   
  for(i=0;i<logger.log.length;i++)
  {
      //alert(logger.log[i]);
  } 
  
  
  var logger2 = new Logger;
  logger2.log; // -> []
  logger2.write('1'); // -> [1]
  logger2.write('2'); // -> [1,2]
  
  
   
  for(i=0;i<logger2.log.length;i++)
  {
      //alert(logger2.log[i]);
  } 


//-----------------------------------------------------------------------------------------------------------------


//-----------------------------------------------------------------------------------------------------------------

var Vulnerable = {
  wound: function(hp) {
    this.health -= hp;
    if (this.health < 0) this.kill();
  },
  kill: function() {
    this.dead = true;
  }
};

// the first argument isn't a class object, so there is no inheritance ...
// simply mix in all the arguments as methods:
var Person = Class.create(Vulnerable, {
  initialize: function() {
    this.health = 100;
    this.dead = false;
	//alert(this.health);
  }
});

var bruce = new Person;
bruce.wound(45);
bruce.health;

//alert(bruce.health);
//alert(bruce.dead);

//-----------------------------------------------------------------------------------------------------------------


//-----------------------------------------------------------------------------------------------------------------

Pirate.allHandsOnDeck = function(n) {
  var voices = [];
  n.times(function(i) {
    voices.push(new Pirate('Sea dog').say(i + 1));
  });
  return voices;
}

alert(Pirate.allHandsOnDeck(3));
//-----------------------------------------------------------------------------------------------------------------
