This is yet another JavaScript class framework. I didn't write it, John Resig did. All I did was package it for npm and format the source in my preferred style.
For the original, see http://ejohn.org/blog/simple-javascript-inheritance/
docs |
This is yet another JavaScript class framework. I didn't write it, John Resig did. All I did was package it for npm and format the source in my preferred style.
For the original, see http://ejohn.org/blog/simple-javascript-inheritance/
/* Simple JavaScript Inheritance | |
* By John Resig http://ejohn.org/ | |
* MIT Licensed. | |
*/ | |
/* | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ | |
// Inspired by base2 and Prototype | |
var initializing = false, fnTest = /xyz/.test(function() | |
{ | |
xyz; | |
}) ? /\b_super\b/ : /.*/; | |
/** | |
* @class Class | |
* The base Class implementation | |
*/ | |
Class = function() {}; | |
/** | |
* @member Class | |
* Create a new Class that inherits from this class | |
* @param {Object} prop Properties to add to the class. | |
*/ | |
Class.extend = function(prop) | |
{ | |
var _super = this.prototype; | |
// Instantiate a base class (but only create the instance, | |
// don't run the init constructor) | |
initializing = true; | |
var prototype = new this(); | |
initializing = false; | |
// Copy the properties over onto the new prototype | |
for(var name in prop) | |
{ | |
// Check if we're overwriting an existing function | |
prototype[name] = typeof prop[name] == "function" && typeof _super[name] == "function" && | |
fnTest.test(prop[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, prop[name]) : prop[name]; | |
} | |
// The dummy class constructor | |
function Class() | |
{ | |
// All construction is actually done in the init method | |
if(!initializing && this.init) | |
{ | |
this.init.apply(this, arguments); | |
} | |
} | |
// Populate our constructed prototype object | |
Class.prototype = prototype; | |
// Enforce the constructor to be what we expect | |
Class.prototype.constructor = Class; | |
// And make this class extendable | |
Class.extend = arguments.callee; | |
return Class; | |
}; | |
module.exports = Class; |
{ | |
"name": "yajscf", | |
"version": "1.0.3", | |
"description": "Yet Another JavaScript Class Framework", | |
"main": "Class.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"repository": { | |
"type": "git", | |
"url": "git+https://gist.github.com/bd2f2aef3db09cd457dc.git" | |
}, | |
"keywords": [ | |
"class", | |
"oop" | |
], | |
"author": "John Resig", | |
"license": "MIT", | |
"bugs": { | |
"url": "https://gist.github.com/bd2f2aef3db09cd457dc" | |
}, | |
"homepage": "https://gist.github.com/bd2f2aef3db09cd457dc" | |
} |