Further discussion of functions used in JavaScript eval ()

Source: Internet
Author: User
Tags javascript eval

Var func = eval ("(function (){})");
Alert (typeof func );
--------
The further problem is that the book does not fully explain the performance of anonymous and named functions in JScript and SpiderMonkey.
Clear. Okay. This article will discuss this issue in depth, not only in the book, but also in depth.
JS interpretation and execution process-in fact, all content is involved in the book, but it is too scattered and not convenient for special
To analyze a specific problem.

First, the expressions and statements should be clarified. For JS, eval () always tries to execute a statement, so it must
First, the execution text is understood as a statement. As follows:
--------
Eval ("1 ")
--------
In JS, because eval () must execute the statement, "1" is no longer a direct expression, but a direct expression language.
Sentence, which is equivalent to "1 ;". These contents include the statements, expressions, and values in the process of dynamic execution 5.2.2"
For more information.

Therefore, the return value of eval () is actually the return value of the last (valid) clause of the statement. Next, we need
You need to know "declaration statement" and "expression ". For example:
--------
Function x (){
//....
}
--------
Obviously, this is a "declaration statement" for a named function ". Note that the "Declaration statement" does not return values. Also
That is to say, the declaration statement is processed by the Pre-compiler during the syntax interpretation period, but it does not make sense during the execution period -- no
There is a value and no return value. For example, a simple "var X" is a declaration statement, and it does not return a value.
For "var X = 100", JS processes a declaration statement and a value assignment statement during execution, which returns
Value (the latter value ).

The above rules are the same for JScript and SpiderMonkey, and there is no difference. The difference is that
Content. First, SpiderMonkey recognizes "function expressions", in order to directly
Feature, which indicates that the "function name" in the "function expression" is invalid. Because "function name" is "Declaration language"
Statement, and the expression is a level smaller (or lower) than the statement, so it is impossible
Statement declaration appears in the expression, so you have to ignore the function name in the expression. In this way, the SpiderMonkey
The following statement:
--------
X = "1234" + (function X (){});
------
Function X does not have an identifier. It does not affect any identifier other than the expression or global identifier.
. Further speaking:
--------
Var X = 100;
X = "1234" + (function X (){});
------
In the two lines of code, the variable X is not overwritten, because the function name X in the second line is invalid. About these
In this document, the question "5.4.2.1 syntax declaration and statement meanings are inconsistent" is explained in detail.

In the above section, we also discussed how MS JScript handles this problem. JScript acknowledges that
The Function Identifier declaration that appears at any location. That is to say, because the above identifier is valid, it is changed globally
The value of "X" is overwritten. However, for this reason, JScript must explain the following question:
------
Eval ("(function (){})");
Eval ("(function X (){})");
------
Are there any differences in semantics between the two lines of code? Since SpiderMonkey recognizes the function expression
Both are interpreted as the operation element of the expression, and JScript must acknowledge the variable name X in the second line of code, so both have to explain
Is a statement. That is to say, by default, JScript considers the first row as the "Declaration statement" of the anonymous function, and the second row as
Is a name function declaration statement. Therefore, as mentioned earlier, "declaration statement" does not return values. Therefore, in JScript
Undefined is returned for all lines of code, and the second line of code declares a variable name X.

With regard to this question, I mentioned in the footer that "the meaning of the function declaration statement" is indeed vague. In any case
It should be simply understood that JScript considers this as "function statement Declaration", while SpiderMonkey considers this as "function expression ".
Statement.

Okay. So far, we can only explain clearly:
------
Eval ("(function (){})");
Eval ("(function X (){})");
------
The effect of the two statements and the reasons for this effect. However, the examples and footer descriptions in my book are still
However, there are doubts. This comes from the text and code:
------
However, there is an exception in JScript: the direct amount of functions (an anonymous function here) cannot be obtained in this way. For example, the following code:
Var func = eval ("(function (){})");
// Output "undefined"
Alert (typeof func );
In this case, you can name the function to get it (*). For example :..................
------
Note that this text is contextual. I just want to explain how to assign a valid value to the variable "func. This involves
And two methods:
------
// Method 1, using an anonymous Function
Var func = eval ("(function (){})");
Alert (typeof func );

// Method 2, appliance name Function
Var func;
Eval ("function func (){}");
Alert (typeof func );
------
This section indicates that "using method 1 In JScript (using anonymous functions) is not acceptable, and the second method is required ",
In the footer, the opposite is true for SpiderMonkey. In this example
The two methods are invalid, while the first method is valid.

This is slightly different from the preceding content. method 2 does not use the "return value", but only uses the eval () Statement
The effect of the function name declaration is used to affect the global variable func. SpiderMonkey does not acknowledge this statement.
Identifier, so it is invalid.

For the same reason, the reader I22141 says to change it to the following:
------
Var func = eval ("function func (){}");
------
In SpiderMonkey, the return value is used, which is different from the preceding example. So I22141
The question "(so you can use eval () to return an anonymous function in SpiderMonkey, but only
What does undefined mean ?)", This is also an example of a question.

Finally, let's talk about a detailed question, which is not mentioned in the book. It is actually a strange event. First
As I said above, In JScript:
--------
// First case
Var func = eval ("(function (){})");
Alert (typeof func); // display "undefined"
--------
Undefined is shown here because JScript interprets the subsequent functions as anonymous function declarations, so there is no value. Actually relative
To be far-fetched. Because we need to modify it:
--------
// The second case
Var func = eval ("(1, function (){})");
Alert (typeof func); // displays "function"
--------
It is different. In the first case, JScript must be understood as a "declaration statement" instead of an expression.
? I am not sure. I just started from:
--------
// Case 3
Var X;
Eval ("(function X (){})");
Alert (typeof X); // displays "function"
--------
In this case, there is a "Statement Declaration" effect to infer the first case. What's more strange is that I don't know why
JScript allows the existence of a "statement" in an expression operator "(...)"-because in theory
It is difficult to explain in syntax analysis. I even raised the question "the syntax declaration of 5.4.2.1 is inconsistent with the meaning of the statement ".
To the following code:
--------
// Example 5: rewrite the syntax declaration stage
// Rewrite
(Function Object (){
}). Prototype. value = 100;

// Display value undefined
Var obj = new Object ();
Alert (obj. value );
--------
This is because the function X in the execution period "(X). prototype. value" overwrites the Global Object ID during the statement analysis period.
Is not the same. However, I still don't understand why JScript allows declaration statements in expressions.
What is more contrary to this is that if you want to acknowledge such assumptions, Why cannot the following statement be executed:
--------
(Var x = 100 );
--------
However, if you do not acknowledge it, the following code can be executed normally:
--------
(Function X (){});
--------
OH... I am still confused about the final answer to this question.

Related Article

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.