prototypes and object properties (Dojo)
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++;
},
};
dojo.declare("Human", null, humanBase);
// Human.prototype == humanBase
var printData = function(obj, prefix){
dojo.create('p', {
'innerHTML': prefix + ' - energy: ' + obj.energy
}, 'result');
};
bob = new Human();
//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');