Class instances now have different default toString() implementations than uninstantiated classes
- Instance id is not yet included, as that is currently in a different branchclosure/master
parent
840a495017
commit
e2cba458d8
13
lib/class.js
13
lib/class.js
|
@ -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
|
||||||
|
|
|
@ -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"
|
||||||
|
);
|
||||||
|
} )();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue