JS Person Class - Deep Prototype Object Modification
JavaScript
var Person = (function() {
function Person( name ) {
this.name = name;
this.attributes = {
head: {
eyes: 2,
nose: 1,
mouth: 1,
ears: 2
}
};
Person.prototype.population++;
}
Person.prototype.population = 0;
return Person;
})();
joe = new Person('Joe');
sue = new Person('Sue');
joe.population = 10;
console.log( 'Set joe.population equal to 10. Creates a population instance property. Does NOT modify prototype.' );
console.log( 'joe.population', joe.population );
console.log( 'sue.population', sue.population );
joe.attributes.head.eyes = 1;
console.log( 'Changed joe.attributes.head.eyes from 2 to 1. Does NOT create a attributes.head.eyes instance property. DOES modify prototype.' );
console.log( 'joe.attributes.head.eyes', joe.attributes.head.eyes );
console.log( 'sue.attributes.head.eyes', sue.attributes.head.eyes );