Recently, I am working on some JavaScript things. Because the system is completely based on configuration files, some JavaScript function names or functions Code Directly written in the configuration file. // Method Name configured
VaR Funcname = " Func1 " ;
// Writing Method
Function Func1 (s ){
Alert (s );
}
It may also be configured as follows:
VaR Funcname = " Function (s) {alert (s );} " ;
Where to call
VaR Str = " Hello! " ;
Eval ( " ( " + Funcname + " ) (STR) " );
Normal call is normal. If compiler is used for compression, the eval will fail because the STR variable has been renamed.
Switch: // VaR F = eval ("(" + funcname + ")");
VaR F = Eval ( " (Function () {return ( " + Funcname + " );})() " );
F. Call ( This , STR );
Solve the problem.
In Chrome/Firefox/opera, Eval ("(" + funcname + ")") can be directly used to return the function object, while IE will returnUndefined.
Use Eval ( " (Function () {return ( " + Funcname + " );})() " ) To solve compatibility issues.