Solutions to running errors after Google closure compiler compresses code using eval

Source: Internet
Author: User
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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.