1
0
Fork 0

Preventing base specification via extend() when implementing off of an existing base class

closure/master
Mike Gerwitz 2011-03-16 19:06:16 -04:00
parent 58cb196213
commit 80f3ec6b68
2 changed files with 34 additions and 0 deletions

View File

@ -361,6 +361,16 @@ function createImplement( base, ifaces, cname )
ext_base = args.pop() ext_base = args.pop()
; ;
// if a base was already provided for extending, don't allow them to
// give us yet another one (doesn't make sense)
if ( base && ext_base )
{
throw Error(
"Cannot override parent " + base.toString() + " with " +
ext_base.toString() + " via extend()"
);
}
// if a name was provided, use it // if a name was provided, use it
if ( cname ) if ( cname )
{ {

View File

@ -189,3 +189,27 @@ var Type = Interface.extend( {
); );
} )(); } )();
/**
* Consider the following scenario:
*
* MyClass.implement( Type ).extend( MyOtherClass, {} );
*
* What the above is essentially saying is: "I'd like to extend MyClass by
* implementing Type. Oh, no, wait, I'd actually like it to extend
* MyOtherClass." That doesn't make sense! Likely, it's unintended. Prevent
* confusion and bugs. Throw an error.
*/
( function testCannotSpecifyParentAfterImplementingAtopExistingClass()
{
assert.throws( function()
{
// should not be permitted
PlainFoo.implement( Type, Type2 ).extend( PlainFoo2, {} );
},
Error,
"Cannot specify new parent for extend() when implementing from " +
"existing class"
);
} )();