1
0
Fork 0

Abstract classes also have a more intuitive string representation

closure/master
Mike Gerwitz 2011-01-17 20:22:30 -05:00
parent d19638be4f
commit afc5d4668d
2 changed files with 21 additions and 1 deletions

View File

@ -338,13 +338,21 @@ var extend = ( function( extending )
// abstract class // abstract class
else else
{ {
return function() var __abstract_self = function()
{ {
if ( !extending ) if ( !extending )
{ {
throw new Error( "Abstract classes cannot be instantiated" ); throw new Error( "Abstract classes cannot be instantiated" );
} }
}; };
// provide a more intuitive string representation
__abstract_self.toString = function()
{
return '<Abstract Class>';
};
return __abstract_self;
} }
} }
} )( false ); } )( false );

View File

@ -207,3 +207,15 @@ assert.throws( function()
} ); } );
}, TypeError, "Abstract methods must be declared as arrays" ); }, TypeError, "Abstract methods must be declared as arrays" );
// otherwise it'll output the internal constructor code, which is especially
// confusing since the user does not write it
( function testConvertingAbstractClassToStringYieldsClassString()
{
assert.equal(
Class.extend( { 'abstract foo': [] } ).toString(),
'<Abstract Class>',
"Converting abstract class to string yields class string"
);
} )();