| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- FC.Class = Class; // export
- // Class that all other classes will inherit from
- function Class() { }
- // Called on a class to create a subclass.
- // Last argument contains instance methods. Any argument before the last are considered mixins.
- Class.extend = function() {
- var len = arguments.length;
- var i;
- var members;
- for (i = 0; i < len; i++) {
- members = arguments[i];
- if (i < len - 1) { // not the last argument?
- mixIntoClass(this, members);
- }
- }
- return extendClass(this, members || {}); // members will be undefined if no arguments
- };
- // Adds new member variables/methods to the class's prototype.
- // Can be called with another class, or a plain object hash containing new members.
- Class.mixin = function(members) {
- mixIntoClass(this, members);
- };
- function extendClass(superClass, members) {
- var subClass;
- // ensure a constructor for the subclass, forwarding all arguments to the super-constructor if it doesn't exist
- if (hasOwnProp(members, 'constructor')) {
- subClass = members.constructor;
- }
- if (typeof subClass !== 'function') {
- subClass = members.constructor = function() {
- superClass.apply(this, arguments);
- };
- }
- // build the base prototype for the subclass, which is an new object chained to the superclass's prototype
- subClass.prototype = createObject(superClass.prototype);
- // copy each member variable/method onto the the subclass's prototype
- copyOwnProps(members, subClass.prototype);
- // copy over all class variables/methods to the subclass, such as `extend` and `mixin`
- copyOwnProps(superClass, subClass);
- return subClass;
- }
- function mixIntoClass(theClass, members) {
- copyOwnProps(members, theClass.prototype);
- }
|