Created
May 14, 2011 18:43
-
-
Save omartell/972498 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
var pseudoClassicalExtend = function(child, parent){ | |
function F(){}; | |
F.prototype = parent.prototype; | |
child.prototype = new F(); | |
child.prototype.constructor = child; | |
child.uber = parent.prototype; | |
}; | |
var prototypalInheritance = function(parent){ | |
function F(){}; | |
F.prototype = parent; | |
return new F(); | |
}; | |
var extendAugment = function(parent, augments){ | |
function F(){}; | |
F.prototype = parent; | |
var child = new F(); | |
child.uber = parent; | |
deepClone(augments, child); | |
return child; | |
}; | |
var shallowClone = function(original, target){ | |
var clone = target || {}; | |
for(var property in original){ | |
clone[property] = original[property]; | |
} | |
return clone; | |
}; | |
var deepClone = function(original, target ){ | |
var clone = target || (original.constructor === Array ? [] : {}); | |
for(var property in original){ | |
if(typeof original[property] === "object"){ | |
clone[property] = deepClone(original[property]) | |
} | |
clone[property] = original[property]; | |
} | |
return clone; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment