Interfaces cannot be instantiated
parent
b357293890
commit
80b0732be1
|
@ -45,40 +45,70 @@ exports.extend = function()
|
||||||
function Interface() {}
|
function Interface() {}
|
||||||
|
|
||||||
|
|
||||||
function extend()
|
var extend = ( function( extending )
|
||||||
{
|
{
|
||||||
var args = Array.prototype.slice.call( arguments ),
|
return function extend()
|
||||||
props = args.pop() || {},
|
{
|
||||||
base = args.pop() || Interface,
|
// ensure we'll be permitted to instantiate interfaces for the base
|
||||||
prototype = new base();
|
extending = true;
|
||||||
|
|
||||||
// sanity check
|
var args = Array.prototype.slice.call( arguments ),
|
||||||
inheritCheck( prototype );
|
props = args.pop() || {},
|
||||||
|
base = args.pop() || Interface,
|
||||||
|
prototype = new base();
|
||||||
|
|
||||||
var new_interface = function() {};
|
// sanity check
|
||||||
|
inheritCheck( prototype );
|
||||||
|
|
||||||
util.propCopy( props, prototype, {
|
var new_interface = createInterface();
|
||||||
each: function( name, value )
|
|
||||||
{
|
util.propCopy( props, prototype, {
|
||||||
if ( util.isAbstractMethod( value ) === false )
|
each: function( name, value )
|
||||||
{
|
{
|
||||||
throw TypeError(
|
if ( util.isAbstractMethod( value ) === false )
|
||||||
"Only abstract methods are permitted within Interface " +
|
{
|
||||||
"definitons"
|
throw TypeError(
|
||||||
);
|
"Only abstract methods are permitted within Interface " +
|
||||||
|
"definitons"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
} );
|
||||||
|
|
||||||
|
attachExtend( new_interface );
|
||||||
|
new_interface.prototype = prototype;
|
||||||
|
new_interface.constructor = new_interface;
|
||||||
|
|
||||||
|
// 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 );
|
||||||
attachExtend( new_interface );
|
|
||||||
new_interface.prototype = prototype;
|
|
||||||
new_interface.constructor = new_interface;
|
|
||||||
|
|
||||||
// freeze the interface (preventing additions), if supported
|
|
||||||
util.freeze( new_interface );
|
|
||||||
|
|
||||||
return new_interface;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -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"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -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 )
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue