From 763b01ef088c5ad6961a5526d35d4613d17505be Mon Sep 17 00:00:00 2001 From: mourner Date: Thu, 2 Sep 2010 17:49:15 +0300 Subject: [PATCH] slight class refactoring --- src/util/Class.js | 55 ++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/src/util/Class.js b/src/util/Class.js index d194d0b9..ff1d31de 100644 --- a/src/util/Class.js +++ b/src/util/Class.js @@ -4,13 +4,36 @@ L.Class = function() {}; +//Thanks to John Resig and Dean Edwards for inspiration L.Class.extend = function(props) { - var _super = this.prototype, statics; - + // extended class with the new prototype + function NewClass() { + if (!L.Class._prototyping && this.initialize) { + this.initialize.apply(this, arguments); + } + } + // instantiate class without calling constructor L.Class._prototyping = true; var proto = new this(); L.Class._prototyping = false; + + proto.constructor = NewClass; + NewClass.prototype = proto; + + // add callParent method + if (this != L.Class) { + var _super = this.prototype; + proto.callParent = function(fnName) { + _super[fnName].apply(this, Array.prototype.slice.call(arguments, 1)); + }; + } + + // mix static properties into the class + if (props.statics) { + L.Util.extend(NewClass, props.statics); + delete props.statics; + } // mix includes into the prototype if (props.includes) { @@ -18,31 +41,10 @@ L.Class.extend = function(props) { delete props.includes; } - // callParent method - if (this != L.Class) { - proto.callParent = function(fnName) { - _super[fnName].apply(this, Array.prototype.slice.call(arguments, 1)); - }; - } - - // save static properties - if (props.statics) { - statics = props.statics; - delete props.statics; - } - // mix given properties into the prototype L.Util.extend(proto, props); - // extended class with the new prototype - function NewClass() { - if (!L.Class._prototyping && this.initialize) { - this.initialize.apply(this, arguments); - } - } - proto.constructor = NewClass; - NewClass.prototype = proto; - + // allow inheriting further NewClass.extend = arguments.callee; // method for adding properties to prototype @@ -50,11 +52,6 @@ L.Class.extend = function(props) { L.Util.extend(this.prototype, props); }; - // mix static properties into the class - if (statics) { - L.Util.extend(NewClass, statics); - } - //inherit parent's statics for (var i in this) { if (this.hasOwnProperty(i) && i != 'prototype') {