Created
December 27, 2010 19:52
-
-
Save jneen/756475 to your computer and use it in GitHub Desktop.
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
// an IO-like clone system for javascript. thoughts? | |
Object.prototype.clone = function() { | |
return Object.create(this); | |
} | |
Object.prototype.merge = function(obj) { | |
var | |
keys = Object.keys(obj), | |
self = this | |
; | |
keys.forEach(function(key) { | |
self[key] = obj[key] | |
}); | |
return this; | |
} | |
Object.prototype.inherit = function(obj) { | |
return this.clone().merge(obj); | |
} | |
mySharedPrototpe = { | |
a: 1, | |
b: function() { return 2; } | |
//etc | |
} | |
function MyConstructor() { | |
// ... | |
} | |
MyConstructor.prototype = mySharedPrototype.inherit({ | |
a: 4, | |
c: "LOL" | |
}); | |
o = new MyConstructor(); | |
o.a // => 4 | |
o.b // => function() { return 2; } | |
o.c // => "LOL" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment