|
@@ -1,15 +1,37 @@
|
|
|
|
|
|
|
|
FC.Class = Class; // export
|
|
FC.Class = Class; // export
|
|
|
|
|
|
|
|
-// class that all other classes will inherit from
|
|
|
|
|
|
|
+// Class that all other classes will inherit from
|
|
|
function Class() { }
|
|
function Class() { }
|
|
|
|
|
|
|
|
-// called upon a class to create a subclass
|
|
|
|
|
-Class.extend = function(members) {
|
|
|
|
|
- var superClass = this;
|
|
|
|
|
- var subClass;
|
|
|
|
|
|
|
|
|
|
- members = members || {};
|
|
|
|
|
|
|
+// 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
|
|
// ensure a constructor for the subclass, forwarding all arguments to the super-constructor if it doesn't exist
|
|
|
if (hasOwnProp(members, 'constructor')) {
|
|
if (hasOwnProp(members, 'constructor')) {
|
|
@@ -32,10 +54,9 @@ Class.extend = function(members) {
|
|
|
copyOwnProps(superClass, subClass);
|
|
copyOwnProps(superClass, subClass);
|
|
|
|
|
|
|
|
return subClass;
|
|
return subClass;
|
|
|
-};
|
|
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
-// 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) {
|
|
|
|
|
- copyOwnProps(members.prototype || members, this.prototype); // TODO: copyNativeMethods?
|
|
|
|
|
-};
|
|
|
|
|
|
|
+
|
|
|
|
|
+function mixIntoClass(theClass, members) {
|
|
|
|
|
+ copyOwnProps(members.prototype || members, theClass.prototype); // TODO: copyNativeMethods?
|
|
|
|
|
+}
|