Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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"
Add Comment
Please, Sign In to add comment