diff --git a/CHANGELOG b/CHANGELOG index de207b3..d70cd78 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Simplify Class.create by establishing a prototype chain when subclassing. Closes #9342. [Ben Newman] + * Fix Ajax.PeriodicalUpdater for compatibility with Ajax.Response. Closes #9321. [kampers] * Ensure that classes always have an initialize method. [Mislav Marohnić] diff --git a/src/base.js b/src/base.js index 922d45c..a543dc9 100644 --- a/src/base.js +++ b/src/base.js @@ -1,34 +1,31 @@ /* Based on Alex Arnell's inheritance implementation. */ var Class = { - create: (function() { - var extending = { }; + create: function(parent, properties) { + if (arguments.length == 1 && !Object.isFunction(parent)) + properties = parent, parent = null; - return function(parent, properties) { - if (arguments.length == 1 && !Object.isFunction(parent)) - properties = parent, parent = null; + function klass() { + this.initialize.apply(this, arguments); + } + + klass.superclass = parent; + klass.subclasses = []; + + if (parent) { + var subclass = function() { }; + subclass.prototype = parent.prototype; + klass.prototype = new subclass; + parent.subclasses.push(klass); + } + + if (properties) Class.extend(klass, properties); + if (!klass.prototype.initialize) + klass.prototype.initialize = Prototype.emptyFunction; - function klass() { - if (arguments[0] !== extending) - this.initialize.apply(this, arguments); - } - - klass.superclass = parent; - klass.subclasses = []; - - if (parent) { - klass.prototype = new parent(extending); - parent.subclasses.push(klass); - } + klass.prototype.constructor = klass; - if (properties) Class.extend(klass, properties); - if (!klass.prototype.initialize) - klass.prototype.initialize = Prototype.emptyFunction; - - klass.prototype.constructor = klass; - - return klass; - }; - })(), + return klass; + }, extend: function(destination, source) { var ancestor = destination.superclass && destination.superclass.prototype; diff --git a/test/unit/base.html b/test/unit/base.html index e1a51cb..23a524c 100644 --- a/test/unit/base.html +++ b/test/unit/base.html @@ -464,11 +464,18 @@ Class.extend(Mouse, { sleep: function($super) { return $super() + " ... no, can't sleep! Gotta steal cheese!"; + }, + escape: function(cat) { + return this.say('(from a mousehole) Take that, ' + cat.name + '!'); } }); assertEqual('Tom: ZZZ', tom.sleep(), "added instance method not available to subclass"); assertEqual("Jerry: ZZZ ... no, can't sleep! Gotta steal cheese!", jerry.sleep()); + assertEqual("Jerry: (from a mousehole) Take that, Tom!", jerry.escape(tom)); + // insure that a method has not propagated *up* the prototype chain: + assertUndefined(tom.escape); + assertUndefined(new Animal().escape); Class.extend(Animal, { sleep: function() {