Fix toString/valueOf sharing same method reference via closure in Class#addMethods. Use plain property assignment, since Object.extend fails to enumerate over toString/valueOf.

This commit is contained in:
Andrew Dupont 2008-05-23 13:30:17 -05:00 committed by Tobie Langel
parent 637134651a
commit 64002a9083
3 changed files with 19 additions and 5 deletions

View File

@ -1,3 +1,5 @@
* Fix toString/valueOf sharing same method reference via closure in Class#addMethods. Use plain property assignment, since Object.extend fails to enumerate over toString/valueOf. (kangax)
* Stop Form.Element.disable from stealing focus. (jddalton)
* Ensure Element.hide and Element.show return an element, even if you pass an element ID. (Andrew Dupont)

View File

@ -44,12 +44,13 @@ Class.Methods = {
var property = properties[i], value = source[property];
if (ancestor && Object.isFunction(value) &&
value.argumentNames().first() == "$super") {
var method = value, value = Object.extend((function(m) {
var method = value;
value = (function(m) {
return function() { return ancestor[m].apply(this, arguments) };
})(property).wrap(method), {
valueOf: function() { return method },
toString: function() { return method.toString() }
});
})(property).wrap(method);
value.valueOf = method.valueOf.bind(method);
value.toString = method.toString.bind(method);
}
this.prototype[property] = value;
}

View File

@ -498,6 +498,17 @@ new Test.Unit.Runner({
valueOf: function() { return "valueOf" }
});
var Parent = Class.create({
m1: function(){ return 'm1' },
m2: function(){ return 'm2' }
});
var Child = Class.create(Parent, {
m1: function($super) { return 'm1 child' },
m2: function($super) { return 'm2 child' }
});
this.assert(new Child().m1.toString().indexOf('m1 child') > -1);
this.assertEqual("toString", new Foo().toString());
this.assertEqual("valueOf", new Foo().valueOf());
}