prototypes and object properties (MooTools)
bob becomes an object of Human. until it's not set bob's properties are just pointers to it's prototype.
by Piotr Zalewa
HTML
<div id='result'></div>
CSS
#result {
margin-top: 10px;
color: navy;
font-weight: bold;
}
JavaScript
var humanBase = {
isAlive: true,
energy: 1,
eat: function(){
this.energy++;
},
}
var Human = new Class(humanBase);
// Human.prototype == humanBase
bob = new Human();
var printData = function(obj, prefix) {
new Element('p',{
'text': prefix + " - energy: {energy}".substitute(obj)
}).inject('result','bottom');
}
// bob.energy == Human.prototype.energy
printData(bob, 'bob');
printData(Human.prototype, 'prototype');
Human.prototype.energy = 2;
// bob.energy == Human.prototype.energy
printData(bob, 'bob');
printData(Human.prototype, 'prototype');
bob.eat();
// bob.energy == 3
printData(bob, 'bob');
printData(Human.prototype, 'prototype');