Skip to content

Instantly share code, notes, and snippets.

@rmkane
Forked from shakyShane/Class.js
Created July 28, 2014 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmkane/248444bf29e11c5d9677 to your computer and use it in GitHub Desktop.
Save rmkane/248444bf29e11c5d9677 to your computer and use it in GitHub Desktop.
/* Simple JavaScript Inheritance for ES 5.1 ( includes polyfill for IE < 9 )
* based on http://ejohn.org/blog/simple-javascript-inheritance/
* (inspired by base2 and Prototype)
* MIT Licensed.
*/
(function (global) {
"use strict";
if (!Object.create) {
Object.create = (function () {
function F() {
}
return function (o) {
if (arguments.length != 1) {
throw new Error("Object.create implementation only accepts one parameter.");
}
F.prototype = o;
return new F();
};
})();
}
var fnTest = /xyz/.test(function () {
xyz;
}) ? /\b_super\b/ : /.*/;
// The base Class implementation (does nothing)
function BaseClass() {
}
// Create a new Class that inherits from this class
BaseClass.extend = function (props) {
var _super = this.prototype;
// Instantiate a base class (but only create the instance,
// don't run the init constructor)
var proto = Object.create(_super);
// Copy the properties over onto the new prototype
for (var name in props) {
// Check if we're overwriting an existing function
proto[name] = typeof props[name] === "function" &&
typeof _super[name] === "function" && fnTest.test(props[name]) ?
(function (name, fn) {
return function () {
var tmp = this._super;
// Add a new ._super() method that is the same method
// but on the super-class
this._super = _super[name];
// The method only need to be bound temporarily, so we
// remove it when we're done executing
var ret = fn.apply(this, arguments);
this._super = tmp;
return ret;
};
})(name, props[name]) :
props[name];
}
// The new constructor
var newClass = typeof proto.init === "function" ?
proto.init : // All construction is actually done in the init method
function () {};
// Populate our constructed prototype object
newClass.prototype = proto;
// Enforce the constructor to be what we expect
proto.constructor = newClass;
// And make this class extendable
newClass.extend = BaseClass.extend;
return newClass;
};
// export
global.Class = BaseClass;
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment