SoFunction
Updated on 2025-04-06

Method and code for obtaining external function names in js class

For example, if we want to set a method in a class, we can save it in a class variable according to the call-in method. When needed, we can obtain it by accessing the class variable.
Usually if we generate an instance
For example: var temp=new TopnetTree();
If we pass in a method by setting properties, we will find that what is passed in is the content of a function, not the function name.
For example =fnTest; //fnTest is a function

So I wrote a method to implement this function.
Use arguments to judge the content passed:
Implement the following functions:
The content passed is empty and no content is executed
Passing a parameter means it is a function without parameters
Passing multiple parameters means the first parameter bit function name, followed by each parameter.

Copy the codeThe code is as follows:

 1 =function(){ 
 2     var fnName,fnArgs=""; 
 3     if(==0){ 
 4         return 0; 
 5     }else if(==1){ 
 6         fnName=arguments[0]; 
 7     }else{ 
 8         fnName=arguments[0]; 
 9         for(var i=1;i<;i++){ 
10             fnArgs+=","+arguments[i]; 
11         } 
12         fnArgs=(",",""); 
13     } 
14          
15     =fnName+"("+fnArgs+")"; 
16 }