Created
August 27, 2012 18:52
-
-
Save pazguille/3491303 to your computer and use it in GitHub Desktop.
Inheritance Patterns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Helpers | |
*/ | |
function clone(obj) { | |
var copy = {}, | |
prop; | |
for (prop in obj) { | |
if (hasOwn(obj, prop)) { | |
copy[prop] = obj[prop]; | |
} | |
} | |
return copy; | |
} | |
function extend(destination, obj) { | |
var prop; | |
for (prop in obj) { | |
destination[prop] = obj[prop]; | |
} | |
return destination; | |
} | |
/** | |
* Inheritance Patterns | |
*/ | |
// Prototype Chaining (pseudo-clasical) | |
function inherits(Child, Parent) { | |
Child.prototype = new Parent(); | |
} | |
// Inherits only the Prototype | |
function inherits(Child, Parent) { | |
Child.prototype = Parent.prototype; | |
} | |
// A temporary constructor | |
function inherits(Child, Parent) { | |
var F = function () {}; | |
F.prototype = Parent.prototype; | |
Child.prototype = new F(); | |
Child.prototype.uber = Parent.prototype; | |
Child.prototype.constructor = Child; | |
} | |
// Copying the prototype properties | |
function inherits(Child, Parent) { | |
var p = Parent.prototype, | |
c = Child.prototype, | |
key; | |
for (key in p) { | |
c[key] = p[key]; | |
} | |
c.uber = p; | |
} | |
// Copy all properties (shallow copy) | |
function inherits(Parent) { | |
var c = {}, | |
key; | |
for (key in p) { | |
c[key] = p[key]; | |
} | |
c.uber = p; | |
return c; | |
} | |
// Prototypal Inheritance | |
function inherits(parent) { | |
function F() {}; | |
F.prototype = parent; | |
return new F(); | |
} | |
// ECMA 5 | |
var child = Object.create(parent); | |
// Extend and augment | |
function inherits(child, parent) { | |
var obj, | |
key; | |
function F() {}; | |
F.prototype = parent; | |
obj = new F(); | |
obj.uber = parent; | |
for (key in child) { | |
obj[i] = child[i]; | |
} | |
return obj; | |
} | |
// Multiple Inheritance | |
function inherits() { | |
var obj = {}, | |
i = 0, | |
stuff, | |
key, | |
len = argument.len; | |
for (;i < len; i += 1) { | |
stuff = arguments[i]; | |
for (key in stuff) { | |
obj[key] = stuff[key]; | |
} | |
} | |
return obj; | |
} | |
// Parasitic inheritance | |
function object() { | |
function F() {}; | |
F.prototype = parent; | |
return new F(); | |
} | |
function inherits(parent) { | |
var that = object(parent); | |
return that; | |
} | |
// Borrowing Constructor | |
function inherits(Child, Parent) { | |
Parent.apply(Child); | |
} | |
// or | |
function Child() { | |
Parent.apply(Child, arguments); | |
} | |
//Borrow a constructor and copy the prototype | |
function Child() { | |
Parent.apply(this, arguments); | |
} | |
extend(Child, Parent); | |
// My proposal | |
function inherits(Child, Parent) { | |
var child = Child.prototype || {}; | |
Child.prototype = extend(child, Parent.prototype); | |
Child.prototype.uber = Parent.prototype; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment