1
0
Fork 0

Class instances now have different default toString() implementations than uninstantiated classes

- Instance id is not yet included, as that is currently in a different branch
closure/master
Mike Gerwitz 2011-03-03 22:53:20 -05:00
parent 840a495017
commit e2cba458d8
2 changed files with 43 additions and 0 deletions

View File

@ -428,6 +428,19 @@ var extend = ( function( extending )
// attach any instance properties/methods (done after // attach any instance properties/methods (done after
// constructor to ensure they are not overridden) // constructor to ensure they are not overridden)
attachInstanceOf( this ); attachInstanceOf( this );
// provide a more intuitive string representation of the class
// instance
this.toString = ( cname )
? function()
{
return 'Object #<' + cname + '>';
}
: function()
{
return 'Object #<anonymous>';
}
;
}; };
// provide a more intuitive string representation // provide a more intuitive string representation

View File

@ -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 #<anonymous>',
"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"
);
} )();