2

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.

dsdsdsdsd
  • 2,880
  • 6
  • 41
  • 56
  • it can't see arguments (against laws of physics), but it could see the formal parameters using `toString()`. look for a Function.name polyfill or just `fn.toString().split(")")[0].split("(")[1].trim().split(/\s*,\s*/)` to get an array of formals. – dandavis Mar 10 '16 at 17:54

1 Answers1

0

I think Angular's method you mention in your question (parsing through the string representation of the function) is the only way to test the parameters of fB. Just for reference, here's the source for how Angular does this parsing.

In your case, however, even this method isn't guaranteed to ensure that fB accepts the parameters you expect - it's possible this method might generate false negatives. For example, according to its signature, this function takes no arguments:

function functionWithNoArguments() {
  document.writeln(arguments[0]);
}

functionWithNoArguments('Hello, world!');

See also: How to get function parameter names/values dynamically from javascript

Community
  • 1
  • 1
Nathan Friend
  • 12,155
  • 10
  • 75
  • 125
  • I don't believe though that fA can test them ... it can get their 'names' ... but beyond that I don't think it can check their expected types ... ... I believe that if this `toString()` procedure is the only way, then it is up to _fB_ to check how fA treats it, and fB handles error-checking .... but whoa that seems wrong ... – dsdsdsdsd Mar 10 '16 at 18:15