Skip to content

Instantly share code, notes, and snippets.

@blasten
Last active August 29, 2015 14:03
Show Gist options
  • Save blasten/be1744070f8c33bfde09 to your computer and use it in GitHub Desktop.
Save blasten/be1744070f8c33bfde09 to your computer and use it in GitHub Desktop.
Extends a function object used as a class Constructor
var extend = function(obj) {
var args = Array.prototype.slice.call(arguments, 1);
for (var i = 0, len = args.length; i < len; i++) {
for (var prop in args[i]) {
obj[prop] = args[i][prop];
}
}
};
NAMESPACE.extend = function(protoProperties) {
var parent = this;
var child = function(){ return parent.apply(this, arguments); };
// Extend static properties to the constructor function
extend(child, parent);
// Set the prototype chain to inherit from `parent`, without calling
// `parent`'s constructor function.
var Dummy = function(){ this.constructor = child; };
Dummy.prototype = parent.prototype;
child.prototype = new Dummy();
// Extend the child's prototype
extend(child.prototype, protoProperties);
// Set a property to point to the parent's prototype
child.__super__ = parent.prototype;
return child;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment