Skip to content

Instantly share code, notes, and snippets.

@jneen
Created December 27, 2010 19:52
Show Gist options
  • Save jneen/756475 to your computer and use it in GitHub Desktop.
Save jneen/756475 to your computer and use it in GitHub Desktop.
// 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