Further discussion of the use of functions in the JavaScript eval () _javascript tips

Source: Internet
Author: User
Tags anonymous javascript eval
var func = eval ("(function () {})");
Alert (typeof func);
--------
Further, the book does not explain the performance of Anonymous and named functions in JScript and SpiderMonkey.
Clear. Well, this article is an in-depth discussion of this issue, not only in the contents of the book, but also in a more in-depth
The next JS interpretation and implementation process-in fact, all the content in the book are involved, but too dispersed, not easy to specialize
To analyze a specific problem.

First, you should explicitly express expressions and statements. For JS, eval () always attempts to execute a statement, so it must
The execution text is first understood as a statement. As follows:
--------
Eval ("1")
--------
In JS's view, since Eval () must execute the statement, "1" is no longer a direct quantity expression, but a direct quantity expression
Sentence, which is equivalent to "1;" This content, in "5.2.2 Dynamic execution of statements, expressions, and values"
is explained in detail.

So, the return value of the eval () is actually the return value of the last (valid) clause of the statement. Next, we need
To learn about declaration statements and expressions. For example:
--------
function X () {
//....
}
--------
Obviously, this is a "declaration statement" of a named function. Note that the "declaration statement" is not a value returned. Also
That is, the declaration statement is processed by the preprocessor during the syntax interpretation period, and it is meaningless during the execution period--No
There is a value and there is no return value. For example, a simple "var X" is a declaration statement, it does not return a value, and for
"Var x=100", JS is processed into a declaration statement, and an assignment statement in the execution period, it has returned
Value (the value of the latter).

The rules above are the same for both JScript and SpiderMonkey, and there is no difference. What's different is that the next
Content. First, SpiderMonkey admits "functional expression (function expression)", in order to direct such a
attribute, which is not valid for the "function name" that appears in the function expression. Because the function name is the declaration
"Expression" is a level that is smaller (or lower) than the statement, and therefore is not possible in the table
"Statement declaration" appears, so you have to ignore the function name in the expression. Thus, in SpiderMonkey
The following statement:
--------
x = "1234" + (function X () {});
------
function x does not have the effect of an identifier, and it does not constitute a shadow of an expression, or a global identifier
Ring. Further to say:
--------
var X = 100;
x = "1234" + (function X () {});
------
In such two lines of code, the variable x is not overridden because the function name x in the second row is invalid. About the inner
In the book, "5.4.2.1 Grammar declaration and statement meaning inconsistent with the question" has detailed explanation.

It is in this section that the handling of Ms JScript about this problem is also discussed. JScript admits in code text
The function identifier declaration that appears at any location in the In other words, because the above identifier is valid, the global variable
The "X" in the quantity will be rewritten. However, it is for this reason that JScript must explain the following question:
------
Eval ("(function () {})");
Eval ("(function X () {})");
------
Excuse me: Is there any semantic difference between the two lines of code? Because SpiderMonkey admits function expressions, so put two of
is interpreted as an operand of an expression, and JScript acknowledges the variable name x in the second line of code, so it has to be explained by two
As the statement. That is, by default, JScript considers the first line to be an anonymous function "declaration statement," and the second line
is a named function declaration statement. Therefore, as previously stated, a declaration statement does not return a value, so in JScript two
The line code returns undefined, and the second line of code declares a variable name x.

In this question, I said in the footnote, "the statement meaning of the function declaration" is indeed somewhat ambiguous. Anyway, just
To simply understand that JScript considers this to be a "function statement declaration", and SpiderMonkey that this is "function expression
Statement "on it.

Good. So far, we have probably only explained:
------
Eval ("(function () {})");
Eval ("(function X () {})");
------
The effect of the two lines of statements, and the reason for this effect. However, for my examples and footnotes in the book, it is still
However, there are questions. This comes from this text and code:
------
However, there is an exception in JScript: The direct amount of a function (this is an anonymous function) cannot be obtained in this way. For example, the following code:
var func = eval ("(function () {})");
Output "undefined"
Alert (typeof func);
In this case, the function can be named to get it (*). For example:..................
------
Note that this text is contextual. I just want to show you how to assign a valid value to the variable "func". This involves
And to two ways:
------
Method 1, using the anonymous function
var func = eval ("(function () {})");
Alert (typeof func);

Method 2, Appliance name function
var func;
Eval ("function func () {}");
Alert (typeof func);
------
This passage means "using Method 1 in JScript (a method with an anonymous function) is not possible and requires a second type",
And in the footnote, say SpiderMonkey is just the opposite, and just refer to this example--in SpiderMonkey, paragraph
Two methods are not valid, and the first one is valid.

Slightly different from what is mentioned here, Method 2 does not use "return value", but only through the eval () statement
The effect of the function name declaration is used to influence the global variable Func. And it is because SpiderMonkey does not recognize this statement.
identifier, so it is invalid.

For the same reason, the reader I22141 said to modify it as follows:
------
var func = eval ("function func () {}");
------
In SpiderMonkey, the return value is used, which is not the same problem as the previous example. So I22141
"(then what you are saying is that you can use Eval () to return an anonymous function in SpiderMonkey, whereas a named function is only
What does it mean to return to undefined? ) "is also a question out of the example.

Finally, to tell a detailed question, which is not mentioned in the book, is actually a very strange event. First, in
Above I said, in JScript:
--------
The first case
var func = eval ("(function () {})");
Alert (typeof func); Show "Undefined"
--------
The undefined is shown here because JScript interprets the following function as an anonymous function declaration, so there is no value. is actually relative
Be a little far-fetched. Because we modify:
--------
Second case
var func = eval ("(1, function () {})");
Alert (typeof func); Show "function"
--------
is different. So why in the first case, JScript must be understood as "declaration statement" rather than expression
What about the formula? I must not know. I just from:
--------
Third situation
var X;
Eval ("(function X () {})");
Alert (typeof X); Show "function"
--------
There is a "statement declaration" effect in this case to infer the first case. What's more peculiar is that I don't know why
What does JScript allow in an expression operator "(...)" There is a "statement"--because theoretically, the feeling
Condition is difficult to explain in the grammatical analysis. I'm even in the "5.4.2.1 Grammar declaration and statement meaning inconsistent with the question" to mention
To the following code:
--------
Example 5: Syntax declaration stage rewrite
Rewrite
(function Object () {
}). Prototype.value = 100;

Display value undefined
var obj = new Object ();
alert (Obj.value);
--------
is because of the function X in the execution period "(x). Prototype.value", which overwrites the global object identifier with the statement parsing period
The function of a character, not the same. However, I still don't understand why JScript allows declarative statements to exist in an expression.
More to the contrary, if you want to acknowledge such an assumption, then why is the following statement not executed:
--------
(Var x=100);
--------
If you do not, however, the following code will perform correctly:
--------
(function X () {});
--------
Oh... I am still confused on the final answer to this question.

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.