From 517b2be46432c570c7f906cc5410dd20e3da8ecd Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Sun, 6 Mar 2011 12:38:31 -0500 Subject: [PATCH] Fix toString() override for IE - All tests once again pass on IE6 --- lib/class.js | 48 +++++++++++++++++++++++++++++++-------- test/test-class-extend.js | 5 ++-- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/lib/class.js b/lib/class.js index ec9e3cc..56e6bdd 100644 --- a/lib/class.js +++ b/lib/class.js @@ -34,6 +34,23 @@ var util = require( './util' ), */ var class_meta = {}; +/** + * IE contains a nasty enumeration "bug" (poor implementation) that makes + * toString unenumerable. This means that, if you do obj.toString = foo, + * toString will NOT show up in `for` or hasOwnProperty(). This is a problem. + * + * This test will determine if this poor implementation exists. + */ +var enum_bug = ( + Object.prototype.propertyIsEnumerable.call( + { toString: function() {} }, + 'toString' + ) === false + ) + ? true + : false +; + /** * This module may be invoked in order to provide a more natural looking class @@ -419,6 +436,15 @@ var extend = ( function( extending ) delete props.__name; } + // IE has problems with toString() + if ( enum_bug ) + { + if ( props.toString !== Object.prototype.toString ) + { + props.__toString = props.toString; + } + } + util.propParse( props, { each: function( name, value, keywords ) { @@ -567,15 +593,19 @@ var extend = ( function( extending ) members[ 'public' ], 'toString' ) ) ) { - this.toString = ( cname ) - ? function() - { - return '[object #<' + cname + '>]'; - } - : function() - { - return '[object #]'; - } + // use __toString if available (see enum_bug), otherwise use + // our own defaults + this.toString = members[ 'public' ].__toString + || ( ( cname ) + ? function() + { + return '[object #<' + cname + '>]'; + } + : function() + { + return '[object #]'; + } + ) ; } }; diff --git a/test/test-class-extend.js b/test/test-class-extend.js index 6def5f0..e5b0675 100644 --- a/test/test-class-extend.js +++ b/test/test-class-extend.js @@ -292,12 +292,13 @@ for ( var i = 0; i < class_count; i++ ) result = '' ; - result = Class( 'Foo', + result = Class( 'FooToStr', { toString: function() { return str; - } + }, + bla: function() {}, })().toString(); assert.equal(