From e087e31621370cf98e421435db75d22319f3f6b8 Mon Sep 17 00:00:00 2001 From: ahocevar Date: Fri, 22 Oct 2010 20:34:21 +0000 Subject: [PATCH] Remove overhead from OpenLayers.Class, using erilem's fancy OpenLayers.inherit function. r=erilem (closes #2899) git-svn-id: http://svn.openlayers.org/trunk/openlayers@10858 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/BaseTypes/Class.js | 100 ++++++++++++++---------------- 1 file changed, 46 insertions(+), 54 deletions(-) diff --git a/lib/OpenLayers/BaseTypes/Class.js b/lib/OpenLayers/BaseTypes/Class.js index 5145fef5e3..bad71334a4 100644 --- a/lib/OpenLayers/BaseTypes/Class.js +++ b/lib/OpenLayers/BaseTypes/Class.js @@ -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 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 @@ OpenLayers.Class.create = function() { }; }; - /** * APIFunction: inherit * *Deprecated*. Old method to inherit from one or more OpenLayers style @@ -102,15 +74,35 @@ OpenLayers.Class.create = function() { * 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