[OpenLayers-Commits] r10858 - trunk/openlayers/lib/OpenLayers/BaseTypes

commits-20090109 at openlayers.org commits-20090109 at openlayers.org
Fri Oct 22 16:34:22 EDT 2010


Author: ahocevar
Date: 2010-10-22 13:34:21 -0700 (Fri, 22 Oct 2010)
New Revision: 10858

Modified:
   trunk/openlayers/lib/OpenLayers/BaseTypes/Class.js
Log:
Remove overhead from OpenLayers.Class, using erilem's fancy OpenLayers.inherit function. r=erilem (closes #2899)


Modified: trunk/openlayers/lib/OpenLayers/BaseTypes/Class.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/BaseTypes/Class.js	2010-10-22 11:11:59 UTC (rev 10857)
+++ trunk/openlayers/lib/OpenLayers/BaseTypes/Class.js	2010-10-22 20:34:21 UTC (rev 10858)
@@ -23,49 +23,22 @@
  *
  */
 OpenLayers.Class = function() {
-    var Class = function() {
-        /**
-         * This following condition can be removed at 3.0 - this is only for
-         * backwards compatibility while the Class.inherit method is still
-         * in use.  So at 3.0, the following three lines would be replaced with
-         * simply:
-         * this.initialize.apply(this, arguments);
-         */
-        if (arguments && arguments[0] != OpenLayers.Class.isPrototype) {
-            this.initialize.apply(this, arguments);
-        }
-    };
-    var extended = {};
-    var parent, initialize, Type;
-    for(var i=0, len=arguments.length; i<len; ++i) {
-        Type = arguments[i];
-        if(typeof Type == "function") {
-            // make the class passed as the first argument the superclass
-            if(i == 0 && len > 1) {
-                initialize = Type.prototype.initialize;
-                // replace the initialize method with an empty function,
-                // because we do not want to create a real instance here
-                Type.prototype.initialize = function() {};
-                // the line below makes sure that the new class has a
-                // superclass
-                extended = new Type();
-                // restore the original initialize method
-                if(initialize === undefined) {
-                    delete Type.prototype.initialize;
-                } else {
-                    Type.prototype.initialize = initialize;
-                }
-            }
-            // get the prototype of the superclass
-            parent = Type.prototype;
-        } else {
-            // in this case we're extending with the prototype
-            parent = Type;
-        }
-        OpenLayers.Util.extend(extended, parent);
+    var len = arguments.length;
+    var P = arguments[0];
+    var F = arguments[len-1];
+
+    var C = typeof F.initialize == "function" ?
+        F.initialize :
+        function(){ P.apply(this, arguments); };
+
+    if (len > 1) {
+        var newArgs = [C, P].concat(
+                Array.prototype.slice.call(arguments).slice(1, len-1), F);
+        OpenLayers.inherit.apply(null, newArgs);
+    } else {
+        C.prototype = F;
     }
-    Class.prototype = extended;
-    return Class;
+    return C;
 };
 
 /**
@@ -90,7 +63,6 @@
     };
 };
 
-
 /**
  * APIFunction: inherit
  * *Deprecated*.  Old method to inherit from one or more OpenLayers style
@@ -102,15 +74,35 @@
  * Returns:
  * An object prototype
  */
-OpenLayers.Class.inherit = function () {
-    var superClass = arguments[0];
-    var proto = new superClass(OpenLayers.Class.isPrototype);
-    for (var i=1, len=arguments.length; i<len; i++) {
-        if (typeof arguments[i] == "function") {
-            var mixin = arguments[i];
-            arguments[i] = new mixin(OpenLayers.Class.isPrototype);
-        }
-        OpenLayers.Util.extend(proto, arguments[i]);
-    }
-    return proto;
+OpenLayers.Class.inherit = function (P) {
+    var C = function() {
+       P.call(this);
+    };
+    var newArgs = [C].concat(Array.prototype.slice.call(arguments));
+    OpenLayers.inherit.apply(null, newArgs);
+    return C.prototype;
 };
+
+/**
+ * Function: OpenLayers.inherit
+ *
+ * Parameters:
+ * C - {Object} the class that inherits
+ * P - {Object} the superclass to inherit from
+ *
+ * In addition to the mandatory C and P parameters, an arbitrary number of
+ * objects can be passed, which will extend C.
+ */
+OpenLayers.inherit = function(C, P) {
+   var F = function() {};
+   F.prototype = P.prototype;
+   C.prototype = new F;
+   var i, l, o;
+   for(i=2, l=arguments.length; i<l; i++) {
+       o = arguments[i];
+       if(typeof o === "function") {
+           o = o.prototype;
+       }
+       OpenLayers.Util.extend(C.prototype, o);
+   }
+};



More information about the Commits mailing list