Using objects the MooTools way

bob becomes an object of Human.

by Piotr Zalewa

HTML

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

CSS

#result {
    margin-top: 10px;
    color: navy;
    font-weight: bold;
}

JavaScript

var Human = new Class({
    isAlive: true,
    energy: 1,
    eat: function(){
        this.energy++;
    },
    printData: function() {
        new Element('p',{
            'text': "energy: {energy}".substitute(this)
        }).inject('result','bottom');
    }
});
bob = new Human();
// bob.energy == 1
bob.printData();
bob.eat();
// bob.energy == 2
bob.printData();