Skip to content

Instantly share code, notes, and snippets.

@ritch
Created July 13, 2011 18:55
Show Gist options
  • Select an option

  • Save ritch/1081024 to your computer and use it in GitHub Desktop.

Select an option

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

rxgx commented Aug 8, 2011

Copy link
Copy Markdown

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