Methods can no longer be overridden with non-methods (anything other than a Function)
parent
c34f40db19
commit
16a91b728e
11
lib/class.js
11
lib/class.js
|
@ -73,6 +73,7 @@ function prop_copy( props, dest )
|
||||||
// care about methods - properties will simply have their values
|
// care about methods - properties will simply have their values
|
||||||
// overwritten)
|
// overwritten)
|
||||||
var pre = dest[ property ],
|
var pre = dest[ property ],
|
||||||
|
prop = props[ property ],
|
||||||
getter = ( ( getset ) ? props.__lookupGetter__( property ) : null ),
|
getter = ( ( getset ) ? props.__lookupGetter__( property ) : null ),
|
||||||
setter = ( ( getset ) ? props.__lookupSetter__( property ) : null );
|
setter = ( ( getset ) ? props.__lookupSetter__( property ) : null );
|
||||||
|
|
||||||
|
@ -92,6 +93,12 @@ function prop_copy( props, dest )
|
||||||
// check for method overrides
|
// check for method overrides
|
||||||
else if ( ( pre !== undefined ) && ( pre instanceof Function ) )
|
else if ( ( pre !== undefined ) && ( pre instanceof Function ) )
|
||||||
{
|
{
|
||||||
|
// ensure we're overriding the method with another method
|
||||||
|
if ( !( prop instanceof Function ) )
|
||||||
|
{
|
||||||
|
throw new TypeError( "Cannot override method with non-method" );
|
||||||
|
}
|
||||||
|
|
||||||
dest[ property ] = ( function( method, super_method )
|
dest[ property ] = ( function( method, super_method )
|
||||||
{
|
{
|
||||||
// this is the method that will be invoked when the requested
|
// this is the method that will be invoked when the requested
|
||||||
|
@ -109,11 +116,11 @@ function prop_copy( props, dest )
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
})( props[ property ], dest[ property ] );
|
})( prop, dest[ property ] );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
dest[ property ] = props[ property ];
|
dest[ property ] = prop;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -118,3 +118,12 @@ assert.equal(
|
||||||
"The parent property may also be used to invoke parent methods"
|
"The parent property may also be used to invoke parent methods"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
assert.throws( function()
|
||||||
|
{
|
||||||
|
Foo.extend(
|
||||||
|
{
|
||||||
|
// overriding method with scalar; shouldn't be allowed
|
||||||
|
myMethod: 'scalar',
|
||||||
|
});
|
||||||
|
}, TypeError, "Methods must be overridden with a Function" );
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue