JavaScript: True Prototypal Inheritance
Untangles JavaScript's constructor pattern, achieving true prototypal inheritance. It takes an old object as a parameter and returns an empty new object that inherits from the old one. If we attempt to obtain a member from the new object, and it lacks that key, then the old object will supply the member.
by sjmcpherson
JavaScript
if (typeof Object.create !== 'function') {
Object.create = function (o) {
function F() {}
F.prototype = o;
return new F();
};
}
var oldObject = {};
var newObject = Object.create(oldObject);
//This object function untangles JavaScript's constructor pattern, achieving true prototypal inheritance. It takes //an old object as a parameter and returns an empty new object that inherits from the old one. If we attempt to //obtain a member from the new object, and it lacks that key, then the old object will supply the member. Objects //inherit from objects. What could be more object oriented than that?