Created
July 13, 2011 18:55
-
-
Save ritch/1081024 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(function(original) { | |
original = original || function(o) { | |
var f = function() { | |
// body... | |
}; | |
f.prototype = o; | |
return new f(); | |
} | |
Object.create = function(obj) { | |
obj.create = original; | |
return original(this); | |
} | |
})(Object.create); | |
var Animal = Object.create({ | |
walk: function() { | |
console.log('walking'); | |
} | |
}); | |
var Dog = Animal.create({ | |
create: function(attributes) { | |
this.created = new Date(); | |
}, | |
bark: function(words) { | |
console.log(words); | |
} | |
}); | |
var sunny = Dog.create({name: 'Sunny'}); | |
console.log(sunny.name); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That's pretty close to the native version of
Object.create
and offers currying of the previous object's create method.