[Effective JavaScript note] 17th: An indirect call to the Eval function is better than a direct call

Source: Internet
Author: User

The Eval function is more than just a function. Most functions only access the scope in which they are defined, but not the scope (lexical scopes) beyond.
The Eval function has the ability to access the entire scope when it is called.
When compiler writers first try to optimize JS, the Eval function makes it difficult to invoke any function efficiently, because once the function that is called is the Eval function, each function call needs to ensure that the entire scope at run time is accessible to the Eval function.
language standards evolve to identify two different ways to call Eval .
The first way: a function call involves an eval identifier, which is considered a "direct" way to invoke the Eval function. The compiler needs to ensure that the executed program has full access to the caller's local scope.
For example:

Other ways: the other way to call the Eval function is called "indirection".
The arguments to the Eval function in two ways are evaluated within the global scope.
For example: Binding the Eval function to another variable name, calling a function through the variable name causes the code to lose access to all local scopes.

The exact definition of the direct call to the Eval function depends on the ECMAScript standard rather special specification language. The only syntax that can produce a direct call to the Eval function is a variable with the name eval that may be wrapped in parentheses (many). A neat way to write an indirect call to the Eval function is to use the expression sequence operator (,) and a distinctly meaningless numeric literal.

(0,eval)(src);

How does this work?
The numeric literal 0 is evaluated but its value is ignored directly, and the result of the sequence expression represented by the parentheses is the Eval function. Thus, the behavior of (0,eval) is almost identical to the simple eval function identifier, an important difference being that the entire invocation expression is treated as a way of indirectly invoking the Eval function.
The ability to invoke the Eval function directly is easily abused.
Evaluating a source string from a network may expose its internal details to some of the untrusted ones.
Calling the Eval function directly causes the function it contains and the risk that all functions that are up to the outermost of the program are running fairly slowly.
Unless there is a clear need to examine the specific capabilities of the local scope, you should use a more inexpensive way to indirectly invoke the Eval function, which is less prone to misuse.

Tips
    • Wrapping the Eval function with a meaningless literal is wrapped in a sequence expression to achieve the purpose of forcing the use of an indirect call to the Eval function

    • Call the Eval function as indirectly as possible, rather than calling the Eval function directly.

[Effective JavaScript note] 17th: An indirect call to the Eval function is better than a direct call

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.