How does a function, fA, that accepts a function, fB, as an argument verify fB's required arguments?
var some_object = { fA : function( fB )
{
// This function will send one parameter, a string, into fB ...
// ... no matter how fB is designed.
// Therefore a mechanism is needed to check fB's arguments.
console.log( "Aa : " + fB ) ;
console.log( "Ab : " + fB.arguments ) ;
console.log( "Ac : " + fB.toString() ) ;
console.log( "Ad : " + typeof fB ) ;
var lva_args = fB.toString().split(")")[0].split("(")[1].trim().split(/\s*,\s*/) ;
console.log( "Ae : " + lva_args) ;
// argn,arga,args ....
// ... but these are just their names,
// and do NOT indicate their type
console.log( "Af : " + typeof lva_args[ 0 ] + " , " + typeof lva_args[ 1 ] + " , " + typeof lva_args[ 2 ] ) ;
// string , string , string
var processed = fB.apply( null , [ 'abc' ] ) ;
return processed ;
}
} ;
var the_result = some_object.fA( function my_any_function( argn , arga , args )
{ console.log( "Ba : " + typeof argn + " , " + typeof arga + " , " + typeof args ) ;
// string , undefined , undefined
return argn * 10 ;
// argn should be a number !!!
// .fA() and my_any_function
// ... are NOT in agreement
}
) ;
console.log( "Z : " + the_result ) ; // --> NaN ... but it could've been worse.
I included the `.toString()' option because I understand that angular converts injected functions into strings and then parses through them to find the arguments, but that must be really messy (thanks @georgeawg).
My plunker.