extendable-base
Version:
Simple library for setting up Javascript classes based on Backbone.extend
53 lines (42 loc) • 1.78 kB
JavaScript
var _ = require('underscore');
/**
* Simple base class that pulls in the extend syntax from Backbone.
*/
var Extendable = function() { };
// (Copy of Backbone.extend)
// Set up the prototype chain, for subclasses.
// Similar to `goog.inherits`, but uses a hash of prototype properties and
// class properties to be extended.
Extendable.extend = function(protoProps, staticProps) {
var parent = this;
var child;
// The constructor function for the new subclass is either defined by you
// (the "constructor" property in your `extend` definition), or defaulted
// by us to simply call the parent's constructor.
if (protoProps && _.has(protoProps, 'constructor')) {
child = protoProps.constructor;
} else {
child = function() { return parent.apply(this, arguments); };
}
// Add static properties to the constructor function, if supplied.
_.extend(child, parent, staticProps);
// Set the prototype chain to inherit from `parent`, without calling
// `parent`'s constructor function.
var Surrogate = function() { this.constructor = child; };
Surrogate.prototype = parent.prototype;
child.prototype = new Surrogate();
// Add prototype properties (instance properties) to the subclass,
// if supplied.
if (protoProps) { _.extend(child.prototype, protoProps); }
// Set a convenience property in case the parent's prototype is needed
// later.
child.__super__ = parent.prototype;
return child;
};
// Set up inheritance with a specific parent class.
Extendable.inherits = function(parent, protoProps, staticProps) {
var child = Extendable.extend.call(parent, protoProps, staticProps);
child.extend = Extendable.extend;
return child;
};
module.exports = Extendable;