1
0
Fork 0

Interfaces cannot be instantiated

closure/master
Mike Gerwitz 2010-12-29 22:40:23 -05:00
parent b357293890
commit 80b0732be1
3 changed files with 66 additions and 30 deletions

View File

@ -45,8 +45,13 @@ exports.extend = function()
function Interface() {} function Interface() {}
function extend() var extend = ( function( extending )
{ {
return function extend()
{
// ensure we'll be permitted to instantiate interfaces for the base
extending = true;
var args = Array.prototype.slice.call( arguments ), var args = Array.prototype.slice.call( arguments ),
props = args.pop() || {}, props = args.pop() || {},
base = args.pop() || Interface, base = args.pop() || Interface,
@ -55,7 +60,7 @@ function extend()
// sanity check // sanity check
inheritCheck( prototype ); inheritCheck( prototype );
var new_interface = function() {}; var new_interface = createInterface();
util.propCopy( props, prototype, { util.propCopy( props, prototype, {
each: function( name, value ) each: function( name, value )
@ -77,8 +82,33 @@ function extend()
// freeze the interface (preventing additions), if supported // freeze the interface (preventing additions), if supported
util.freeze( new_interface ); util.freeze( new_interface );
// we're done; let's not allow interfaces to be instantiated anymore
extending = false;
return new_interface; return new_interface;
};
/**
* Creates a new interface constructor function
*
* @return {function()}
*/
function createInterface()
{
return function()
{
// allows us to extend the interface without throwing an exception
// (since the prototype requires an instance)
if ( !extending )
{
// only called if someone tries to create a new instance of an
// interface
throw Error( "Interfaces cannot be instantiated" );
} }
};
}
} )( false );
/** /**

View File

@ -75,7 +75,7 @@ var SubType = Interface.extend( BaseType,
}); });
assert.ok( assert.ok(
( new SubType() instanceof BaseType ), ( SubType.prototype instanceof BaseType ),
"Generic interface extend method can extend from other interfaces" "Generic interface extend method can extend from other interfaces"
); );
@ -102,7 +102,7 @@ var SubType2 = BaseType.extend(
}); });
assert.ok( assert.ok(
( new SubType2 instanceof BaseType ), ( SubType2.prototype instanceof BaseType ),
"Interface extend method can extend interfaces" "Interface extend method can extend interfaces"
); );

View File

@ -35,6 +35,12 @@ assert.ok(
"Interface extend method creates a new interface object" "Interface extend method creates a new interface object"
); );
assert.throws( function()
{
new FooType();
}, Error, "Interfaces cannot be instantiated" );
// only perform check if supported by the engine // only perform check if supported by the engine
if ( Object.isFrozen ) if ( Object.isFrozen )
{ {