diff --git a/lib/class.js b/lib/class.js index ccea8a3..ca46b5d 100644 --- a/lib/class.js +++ b/lib/class.js @@ -73,6 +73,7 @@ function prop_copy( props, dest ) // care about methods - properties will simply have their values // overwritten) var pre = dest[ property ], + prop = props[ property ], getter = ( ( getset ) ? props.__lookupGetter__( property ) : null ), setter = ( ( getset ) ? props.__lookupSetter__( property ) : null ); @@ -92,6 +93,12 @@ function prop_copy( props, dest ) // check for method overrides 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 ) { // this is the method that will be invoked when the requested @@ -109,11 +116,11 @@ function prop_copy( props, dest ) return retval; } - })( props[ property ], dest[ property ] ); + })( prop, dest[ property ] ); } else { - dest[ property ] = props[ property ]; + dest[ property ] = prop; } } } diff --git a/test/test-class-parent.js b/test/test-class-parent.js index 4b1a246..e05be78 100644 --- a/test/test-class-parent.js +++ b/test/test-class-parent.js @@ -118,3 +118,12 @@ assert.equal( "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" ); +