Functional inheritance pattern
by julienrf
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) {
var that = base(spec); // Create the object through the "base" constructor
that.sayHello = function() { // Augment that object
return 'Hello, I\'m ' + that.name;
};
return that; // Return it
}
// Usage
var object = child({ name: 'a functional object' });
result.textContent = object.sayHello();