Skip to content

Instantly share code, notes, and snippets.

@sbstp
Created March 11, 2014 18:32
Show Gist options
  • Save sbstp/9492068 to your computer and use it in GitHub Desktop.
Save sbstp/9492068 to your computer and use it in GitHub Desktop.
// allow object inheritance
Object.extends = function (ctor, superCtor) {
// collect objects already on the prototype to "save" them
var descriptors = {};
// copy properties from old prototype
Object.getOwnPropertyNames(ctor.prototype).forEach(function (propertyName) {
descriptors[propertyName] = Object.getOwnPropertyDescriptor(ctor.prototype, propertyName);
}, this);
// proto
var newProto = Object.create(superCtor.prototype, descriptors);
ctor.prototype = newProto;
ctor.$super = superCtor;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment