-
-
Save AlexTiTanium/7151518 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
"use strict"; | |
var Toolbox = window.Toolbox = {}; | |
// `ctor` and `inherits` are from Backbone (with some modifications): | |
// http://documentcloud.github.com/backbone/ | |
// Shared empty constructor function to aid in prototype-chain creation. | |
var ctor = function () {}; | |
// Helper function to correctly set up the prototype chain, for subclasses. | |
// Similar to `goog.inherits`, but uses a hash of prototype properties and | |
// class properties to be extended. | |
var inherits = function (parent, protoProps, staticProps) { | |
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 `super()`. | |
if (protoProps && protoProps.hasOwnProperty('constructor')) { | |
child = protoProps.constructor; | |
} else { | |
child = function () { return parent.apply(this, arguments); }; | |
} | |
// Inherit class (static) properties from parent. | |
_.extend(child, parent); | |
// Set the prototype chain to inherit from `parent`, without calling | |
// `parent`'s constructor function. | |
ctor.prototype = parent.prototype; | |
child.prototype = new ctor(); | |
// Add prototype properties (instance properties) to the subclass, | |
// if supplied. | |
if (protoProps) _.extend(child.prototype, protoProps); | |
// Add static properties to the constructor function, if supplied. | |
if (staticProps) _.extend(child, staticProps); | |
// Correctly set child's `prototype.constructor`. | |
child.prototype.constructor = child; | |
// Set a convenience property in case the parent's prototype is needed later. | |
child.__super__ = parent.prototype; | |
return child; | |
}; | |
// Self-propagating extend function. | |
// Create a new class that inherits from the class found in the `this` context object. | |
// This function is meant to be called in the context of a constructor function. | |
function extendThis(protoProps, staticProps) { | |
var child = inherits(this, protoProps, staticProps); | |
child.extend = extendThis; | |
return child; | |
} | |
// A primitive base class for creating subclasses. | |
// All subclasses will have the `extend` function. | |
// Example: | |
// var MyClass = Toolbox.Base.extend({ | |
// someProp: 'My property value', | |
// someMethod: function () { ... } | |
// }); | |
// var instance = new MyClass(); | |
Toolbox.Base = function () {} | |
Toolbox.Base.extend = extendThis; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment