Preventing base specification via extend() when implementing off of an existing base class
parent
58cb196213
commit
80f3ec6b68
10
lib/class.js
10
lib/class.js
|
@ -361,6 +361,16 @@ function createImplement( base, ifaces, cname )
|
|||
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 ( cname )
|
||||
{
|
||||
|
|
|
@ -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"
|
||||
);
|
||||
} )();
|
||||
|
||||
|
|
Loading…
Reference in New Issue