Interfaces cannot be instantiated
parent
b357293890
commit
80b0732be1
|
@ -45,8 +45,13 @@ exports.extend = function()
|
|||
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 ),
|
||||
props = args.pop() || {},
|
||||
base = args.pop() || Interface,
|
||||
|
@ -55,7 +60,7 @@ function extend()
|
|||
// sanity check
|
||||
inheritCheck( prototype );
|
||||
|
||||
var new_interface = function() {};
|
||||
var new_interface = createInterface();
|
||||
|
||||
util.propCopy( props, prototype, {
|
||||
each: function( name, value )
|
||||
|
@ -77,8 +82,33 @@ function extend()
|
|||
// freeze the interface (preventing additions), if supported
|
||||
util.freeze( new_interface );
|
||||
|
||||
// we're done; let's not allow interfaces to be instantiated anymore
|
||||
extending = false;
|
||||
|
||||
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 );
|
||||
|
||||
|
||||
/**
|
||||
|
|
|
@ -75,7 +75,7 @@ var SubType = Interface.extend( BaseType,
|
|||
});
|
||||
|
||||
assert.ok(
|
||||
( new SubType() instanceof BaseType ),
|
||||
( SubType.prototype instanceof BaseType ),
|
||||
"Generic interface extend method can extend from other interfaces"
|
||||
);
|
||||
|
||||
|
@ -102,7 +102,7 @@ var SubType2 = BaseType.extend(
|
|||
});
|
||||
|
||||
assert.ok(
|
||||
( new SubType2 instanceof BaseType ),
|
||||
( SubType2.prototype instanceof BaseType ),
|
||||
"Interface extend method can extend interfaces"
|
||||
);
|
||||
|
||||
|
|
|
@ -35,6 +35,12 @@ assert.ok(
|
|||
"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
|
||||
if ( Object.isFrozen )
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue