Skip to content

Instantly share code, notes, and snippets.

@ritch
Created July 13, 2011 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ritch/1081024 to your computer and use it in GitHub Desktop.
Save ritch/1081024 to your computer and use it in GitHub Desktop.
(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);
@rxgx
Copy link

rxgx commented Aug 8, 2011

That's pretty close to the native version of Object.create and offers currying of the previous object's create method.

if (typeof Object.create !==
 "function") {
  Object.create = function (object) {
    function F() {}

    F.prototype = object;

    return new F();
  };
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment