prototype: Fix superclass method call breakage from [7337].

This commit is contained in:
Sam Stephenson 2007-08-18 22:55:35 +00:00
parent 702d968841
commit b923d6dd52
3 changed files with 39 additions and 7 deletions

View File

@ -1,5 +1,7 @@
*SVN*
* Fix superclass method call breakage from [7337]. [Mislav Marohnić, sam]
* Change Class.extend to allow for superclass method resolution and remove Class.inherit. Closes #9274. [Samuel Lebeau]
* Event handlers are now bound to the observed element, not the event's target element. [Dan Webb]

View File

@ -15,7 +15,7 @@ var Class = {
klass.superclass = parent;
klass.subclasses = [];
if (Object.isFunction(parent)) {
if (parent) {
klass.prototype = new parent(extending);
parent.subclasses.push(klass);
}
@ -34,9 +34,9 @@ var Class = {
var value = source[property];
if (ancestor && Object.isFunction(value) &&
value.argumentNames().first() == "$super") {
var method = value, value = Object.extend((function() {
return ancestor[property].apply(this, arguments)
}).wrap(method), {
var method = value, value = Object.extend((function(m) {
return function() { return ancestor[m].apply(this, arguments) };
})(property).wrap(method), {
valueOf: function() { return method },
toString: function() { return method.toString() }
});

View File

@ -398,7 +398,20 @@
}
}},
testInstantiation: function() { with(this) {
testClassCreate: function() { with(this) {
assert(Object.isFunction(Animal), 'Animal is not a constructor');
assertEnumEqual([Cat, Mouse], Animal.subclasses);
Animal.subclasses.each(function(subclass) {
assertEqual(Animal, subclass.superclass);
});
var Bird = Class.create(Animal);
assertEqual(Bird, Animal.subclasses.last());
// for..in loop (for some reason) doesn't iterate over the constructor property in top-level classes
assertEnumEqual(Object.keys(new Animal), Object.keys(new Bird).without('constructor'));
}},
testClassInstantiation: function() { with(this) {
var pet = new Animal("Nibbles");
assertEqual("Nibbles", pet.name, "property not initialized");
assertEqual('Nibbles: Hi!', pet.say('Hi!'));
@ -415,12 +428,29 @@
assertEqual('Tom: Yuk! I only eat mice.', tom.eat(new Animal));
}},
testSupercall: function() { with(this) {
testSuperclassMethodCall: function() { with(this) {
var tom = new Cat('Tom');
assertEqual('Tom: Yum!', tom.eat(new Mouse));
// augment the constructor and test
var Dodo = Class.create(Animal, {
initialize: function($super, name) {
$super(name);
this.extinct = true;
},
say: function($super, message) {
return $super(message) + " honk honk";
}
});
var gonzo = new Dodo('Gonzo');
assertEqual('Gonzo', gonzo.name);
assert(gonzo.extinct, 'Dodo birds should be extinct');
assertEqual("Gonzo: hello honk honk", gonzo.say("hello"));
}},
testAddingInstanceMethod: function() { with(this) {
testClassExtend: function() { with(this) {
var tom = new Cat('Tom');
var jerry = new Mouse('Jerry');