diff --git a/lib/class.js b/lib/class.js index 3637a33..ce480fc 100644 --- a/lib/class.js +++ b/lib/class.js @@ -428,6 +428,19 @@ var extend = ( function( extending ) // attach any instance properties/methods (done after // constructor to ensure they are not overridden) attachInstanceOf( this ); + + // provide a more intuitive string representation of the class + // instance + this.toString = ( cname ) + ? function() + { + return 'Object #<' + cname + '>'; + } + : function() + { + return 'Object #'; + } + ; }; // provide a more intuitive string representation diff --git a/test/test-class-name.js b/test/test-class-name.js index da600ae..c5237e6 100644 --- a/test/test-class-name.js +++ b/test/test-class-name.js @@ -98,3 +98,33 @@ var common = require( './common' ), ); } )(); + +/** + * Class instances are displayed differently than uninstantiated classes. + * Mainly, they output that they are an object, in addition to the class name. + */ +( function testConvertingClassInstanceToStringYieldsInstanceString() +{ + var name = 'Foo', + + anon = Class( {} )(), + named = Class( name, {} )() + ; + + // anonymous + assert.equal( + anon.toString(), + 'Object #', + "Converting anonymous class instance to string yields string " + + "indiciating that the class is anonymous" + ); + + // named + assert.equal( + named.toString(), + 'Object #<' + name + '>', + "Converting named class instance to string yields string with name " + + "of class" + ); +} )(); +