Refactored method override logic into its own function
parent
1a9cc40c31
commit
3f169d87ea
63
lib/class.js
63
lib/class.js
|
@ -172,31 +172,9 @@ function prop_copy( props, dest, result_data )
|
||||||
// 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
|
dest[ property ] = method_override( pre, prop );
|
||||||
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
|
|
||||||
// method is called, so note that in the context of this
|
|
||||||
// function, `this` will represent the current class instance
|
|
||||||
return function()
|
|
||||||
{
|
|
||||||
var tmp = this.__super;
|
|
||||||
|
|
||||||
// assign _super temporarily for the method invocation so
|
|
||||||
// that the method can call the parent method
|
|
||||||
this.__super = super_method;
|
|
||||||
var retval = method.apply( this, arguments );
|
|
||||||
this.__super = tmp;
|
|
||||||
|
|
||||||
return retval;
|
|
||||||
}
|
|
||||||
})( prop, dest[ property ] );
|
|
||||||
}
|
}
|
||||||
|
// just copy over the property
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
dest[ property ] = prop;
|
dest[ property ] = prop;
|
||||||
|
@ -212,6 +190,42 @@ function prop_copy( props, dest, result_data )
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overrides a method
|
||||||
|
*
|
||||||
|
* The given method must be a function or an exception will be thrown.
|
||||||
|
*
|
||||||
|
* @param {Function} super_method method to override
|
||||||
|
* @param {Function} new_method method to override with
|
||||||
|
*
|
||||||
|
* @return {Function} overridden method
|
||||||
|
*/
|
||||||
|
function method_override( super_method, new_method )
|
||||||
|
{
|
||||||
|
// ensure we're overriding the method with another method
|
||||||
|
if ( !( new_method instanceof Function ) )
|
||||||
|
{
|
||||||
|
throw new TypeError( "Cannot override method with non-method" );
|
||||||
|
}
|
||||||
|
|
||||||
|
// this is the method that will be invoked when the requested
|
||||||
|
// method is called, so note that in the context of this
|
||||||
|
// function, `this` will represent the current class instance
|
||||||
|
return function()
|
||||||
|
{
|
||||||
|
var tmp = this.__super;
|
||||||
|
|
||||||
|
// assign _super temporarily for the method invocation so
|
||||||
|
// that the method can call the parent method
|
||||||
|
this.__super = super_method;
|
||||||
|
var retval = new_method.apply( this, arguments );
|
||||||
|
this.__super = tmp;
|
||||||
|
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shrinks an array, removing undefined elements
|
* Shrinks an array, removing undefined elements
|
||||||
*
|
*
|
||||||
|
@ -422,3 +436,4 @@ function attach_extend( func )
|
||||||
// less restrictive)
|
// less restrictive)
|
||||||
define_secure_prop( func, 'extend', ext_method );
|
define_secure_prop( func, 'extend', ext_method );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue