Functional inheritance pattern
HTML
<div id=result></div>
JavaScript
// Base object constructor function
function base(spec) {
var that = {}; // Create an empty object
that.name = spec.name; // Add it a "name" property
return that; // Return the object
}
// Construct a child object, inheriting from "base"
function child(spec) {
base.apply(this, arguments);
/* var that = {} */ //= base(spec); // Create the object through the "base" constructor
/* that.name = spec.name; */
this.sayHello = function() { // Augment that object
return 'Hello, I\'m ' + this.name;
};
/* return that */; // Return it}
}
// Usage
var object = child({ name: 'a sdf functional object' });
result.textContent = object.sayHello();