


var func = eval("(function(){})");
alert(typeof func);
————————————
A further problem is that the book treats anonymous and The behavior of named functions in JScript and SpiderMonkey is not explained
clearly enough. Okay, this article discusses this issue in depth. It not only involves the content in the book, but also tells a more in-depth
explanation and execution process of JS - in fact, all the content is covered in the book, but it is too Scattered, it is inconvenient to analyze a specific problem specifically
.
First of all, expressions and statements should be made clear. For JS, eval() always tries to execute a statement, so it must
understand the execution text as a statement first. As follows:
---------
eval("1")
---------
From the perspective of JS, since eval() must execute the statement, so "1" is no longer a direct expression, but a direct expression sentence, which is equivalent to "1;". These contents are explained in detail in "5.2.2 Statements, expressions and values during dynamic execution"
.
So, the return value of eval() is actually the return value of the last (valid) clause of the statement. Next, we need to
understand "declaration statements" and "expressions". For example:
───────
function x() {
//....
}
───────
Obviously , which is the "declaration statement" of a named function.Note that the "declaration statement" does not return a value. In other words, the declaration statement is processed by the precompiler during the syntax interpretation period, but it is meaningless during the execution period - it has no
value and no return value. For example, simply "var , it will return the
value (the latter value).
The above rules are the same for JScript and SpiderMonkey, there is no difference. The difference is the following
content. First of all, SpiderMonkey recognizes "function expression". In order to directly implement such a
feature, it assumes that the "function name" appearing in the "function expression" is invalid. Because the "function name" is stated in a "declaration
sentence", and the "expression" is a level smaller (or lower level) than the statement, it cannot be in the "expression
expression" "Statement declaration" appears, so I have to ignore the function name in the expression. In this way, the
following statement in SpiderMonkey:
--------
x = "1234" (function X() {});
------
The function To put it further:
--------
var X = 100;
x = "1234" (function
In these two lines of code, the variable X will not be rewritten because the function name X in the second line is invalid. Regarding these contents
, there is a detailed explanation in the book "5.4.2.1 Problems with Inconsistency between Grammatical Statements and Statement Meanings".
It is in the above section that MS JScript's handling of this problem is also discussed. JScript recognizes function identifier declarations appearing anywhere within the code body
. In other words, since the above identifier is valid, the "X" in the global variable
will be rewritten. However, it is for this reason that JScript must explain the following problem:
----
eval("(function(){})");
eval("( function Since SpiderMonkey recognizes function expressions, it interprets both
as operands of the expression; and JScript needs to recognize the variable name X in the second line of code, so it has to interpret both
as statements. In other words, by default, JScript considers the first line to be an anonymous function "declaration statement" and the second line
to be a named function declaration statement. Therefore, as mentioned before, the "declaration statement" does not return a value, so both
lines of code in JScript return undefined, and the second line of code declares a variable name X.
Regarding this issue, I said in the footnote that the "statement meaning of function declaration" is indeed a bit vague. In any case, just
should be simply understood as JScript thinks this is a "function statement declaration", and SpiderMonkey thinks this is a "function expression
declaration".
Okay. So far, we have probably only explained it clearly:
----
eval("(function(){})");
eval("(function X(){}) ");
------
The effect of these two lines of statements and the reasons for this effect. However, I still have questions about the examples and footnotes in my book. This comes from this text and code:
————————
However, there is an exception in JScript: function literals (here anonymous functions) cannot be obtained in this way. For example, the following code:
var func = eval("(function() { })");
// Output "undefined"
alert(typeof func);
In this case, It can be obtained using named functions (*). For example: ………………
------
First note that this text is context-sensitive. I just want to show how you can assign a valid value to the variable "func". This involves
two methods:
-----
//Method 1, use anonymous function
var func = eval("(function() { })");
alert(typeof func);
// Method 2, use named function
var func;
eval("function func() { }");
alert(typeof func);
------
The meaning of this passage is "It is not possible to use method 1 (using anonymous functions) in JScript, you need to use the second method",
And In the footnote, it is said that SpiderMonkey is just the opposite, and it only refers to this example - in SpiderMonkey, the second method is invalid, but the first method is valid.
What is slightly different from the previous content is that method 2 does not use the "return value", but only uses the effect of the function name declaration in the eval() statement to affect the global variable func . And it is precisely because SpiderMonkey does not recognize the
identifier of this declaration, so it is invalid.
For the same reason, reader I22141 said to change it to the following:
---- ----
In SpiderMonkey, the return value is used, which is no longer the same problem as the above example. So what I22141
asked "(So what do you mean when you say that eval() can be used to return an anonymous function in SpiderMonkey, but named functions can only
return undefined?)" is also divorced from this An example question.
Finally, let’s talk about a detail issue. This is not mentioned in the book. In fact, it is also a very weird incident. First of all, in
I said above that in JScript:
--------
// The first case
var func = eval("(function(){}) ");
alert(typeof func); // Display "undefined"
--------
Undefined is displayed here because JScript interprets the following function as an anonymous function declaration, so there is no value. In fact, it is a bit more far-fetched than
. Because let’s modify it:
————————————
// The second case
var func = eval("(1, function(){})");
alert (typeof func); // Display "function"
------
is different. So why in the first case, JScript must be understood as a "declaration statement" rather than an expression? I don't know for sure. I just start from:
--------
// The third case
var X;
eval("(function X(){})");
alert(typeof What's even more strange is that I don't know why JScript allows a "statement" within an expression operator "(...)" - because theoretically, this situation
It is difficult to interpret in grammatical analysis. I even mentioned the following code in "5.4.2.1 The problem of inconsistency between syntax declaration and statement meaning":
---------
// Example 5: Rewriting in the syntax declaration stage
// Rewrite
(function Object(){
}).prototype.value = 100;
// Display value undefined
var obj = new Object();
alert(obj.value);
--------
is because the function X in "(X).prototype.value" during the execution period and the statement analysis period cover the global The functions of the Object identifier
are not the same. But, I still don't understand why JScript allows declaration statements in expressions.
What is even more contrary to this is that if such an assumption is to be admitted, then why the following statement cannot be executed:
---------
(var x=100);
-------- 🎜>--------
OH... I am still confused as to the final answer to this question.

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
