(function(){
function Humanity() {
this.person = "joe";
}
function Individual(){
this.Run = function(fromWhere){
alert(this.person + ' runs from ' + fromWhere + '!');
}
}
Individual.prototype = new Humanity;
var individual = new Individual;
individual.Run('bear');
})()
Instead of relying on static class-type definitions, JavaScript utilizes functions to dynamically construct data structure prototypes. This approach allows for building a structure as needed, based on gathered context. The dynamic nature of the prototype chain is a significant advancement that I am still trying to fully grasp.
For a clearer understanding, observe the following source code:
(function(){
// Example of prototype chaining
function part1(){this.foo = "foo"}
function part2(){this.bar = "bar"}
function part3(){this.bat = "bat"}
part2.prototype = new part1();
part3.prototype = new part2();
var x = new part1;
var y = new part2;
var z = new part3;
// Inherited state
var memberList = [
x.foo, // "foo"
x.bar, // undefined
x.bat, // undefined
y.foo, // "foo"
y.bar, // "bar"
y.bat, // undefined
z.foo, // "foo"
z.bar, // "bar"
z.bat // "bat"
];
// Chained prototypes
var instanceList = [
x instanceof part1, // true
x instanceof part2, // false
x instanceof part3, // false
y instanceof part1, // true
y instanceof part2, // true
y instanceof part3, // false
z instanceof part1, // true
z instanceof part2, // true
z instanceof part3 // true
];
// Attempt to break the chain
function part4(){this.fu = "fu"}
part2.prototype = new part4;
// State remains consistent
var memberList2 = [
x.foo, // "foo"
x.bar, // undefined
x.bat, // undefined
y.foo, // "foo"
y.bar, // "bar"
y.bat, // undefined
z.foo, // "foo"
z.bar, // "bar"
z.bat // "bat"
];
// Chain remains unbroken, but link is removed
var instanceList2 = [
x instanceof part1, // true
x instanceof part2, // false
x instanceof part3, // false
y instanceof part1, // true
y instanceof part2, // false
y instanceof part3, // false
z instanceof part1, // true
z instanceof part2, // false
z instanceof part3 // true
];
// No new link is added
var instanceList3 = [
x instanceof part4, // false
y instanceof part4, // false
z instanceof part4 // false
];
debugger
})()