From 9b78edb9deb8e0fa2476afdf55e68ea8ca3a2edc Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Tue, 6 Nov 2007 01:44:24 +0000 Subject: [PATCH] prototype: Fix Class#addMethods for "toString" and "valueOf" methods in Internet Explorer. Closes #9901. --- CHANGELOG | 2 ++ src/base.js | 12 ++++++++---- test/unit/base.html | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index d0bea47..6d82a0a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fix Class#addMethods for "toString" and "valueOf" methods in Internet Explorer. Closes #9901. [sam] + * Exclude Opera from using the sourceIndex approach in Element#descendantOf. [Tobie Langel, Andrew Dupont] * Ensure Element#hasClassName always returns a boolean. Closes #10075. [ronnylovtangen, Tobie Langel] diff --git a/src/base.js b/src/base.js index f73c9f9..cda0435 100644 --- a/src/base.js +++ b/src/base.js @@ -34,10 +34,14 @@ var Class = { Class.Methods = { addMethods: function(source) { - var ancestor = this.superclass && this.superclass.prototype; - - for (var property in source) { - var value = source[property]; + var ancestor = this.superclass && this.superclass.prototype; + var properties = Object.keys(source); + + if (!Object.keys({ toString: true }).length) + properties.push("toString", "valueOf"); + + for (var i = 0, length = properties.length; i < length; i++) { + var property = properties[i], value = source[property]; if (ancestor && Object.isFunction(value) && value.argumentNames().first() == "$super") { var method = value, value = Object.extend((function(m) { diff --git a/test/unit/base.html b/test/unit/base.html index 12a8d80..267963e 100644 --- a/test/unit/base.html +++ b/test/unit/base.html @@ -599,6 +599,21 @@ assertEqual('#', cow.inspect()); assertRespondsTo('reproduce', cow); assertRespondsTo('getValue', cow); + }}, + + testClassWithToStringAndValueOfMethods: function() { with(this) { + var Foo = Class.create({ + toString: function() { + return "toString"; + }, + + valueOf: function() { + return "valueOf"; + } + }); + + assertEqual("toString", new Foo().toString()); + assertEqual("valueOf", new Foo().valueOf()); }} }, 'testlog');