Mixins with MooTools

Ninja and Samurai are extending the Human mixing it with Warrior

by Piotr Zalewa

HTML

<div id='result'></div>

CSS

#result {
    margin-top:  10px;
    color: navy;
    font-weight:  bold;
    font-size: 11px;
    font-family:  Helvetica, Tahoma, Arial;
}

JavaScript

var printData = function(obj) {
    new Element('p',{
        'text': "{name} ({age}) - energy: {energy}, alive: {isAlive}".substitute(obj)
    }).inject('result','bottom');
};

// ------------------------------------------
var Human = new Class({
    initialize: function(name, age){
        this.name = name;
        this.age = age;
    },
    isAlive: true,
    energy: 1,
    eat: function(){
        this.energy++;
    }
});
// ------------------------------------------
var Warrior = new Class({
    energy: 100,
    kills: 0,
    attack: function(target){
        if (target.energy < this.energy) {
            target.isAlive = false;
            this.kills++;
        }
        this.energy = this.energy - 5;
    }
});
// ------------------------------------------
var Ninja = new Class({
    Extends: Human,
    Implements: [Warrior],
    initialize: function(side, name, age){
        this.side = side;
        this.parent(name, age);
    }
});
// ------------------------------------------
var Samurai = new Class({
    Extends: Human,
    Implements: [Warrior],
    side: 'good',
    energy: 1000
});

/*
This  is were you should start to play
*/