The “new function” Syntax

JavaScript The “new function” Syntax (JavaScript “new function”语法)

There is another way of creating a function. It’s not used often, but in times, there is no alternative. (还有另一种方法可以创建函数。它不经常使用,但有时别无选择。)

Here is the syntax that can be used for function-creating:

let fn = new Function([arg1, arg2, ...argN], functionBody);

It is created with the following arguments: arg1…argN and the functionBody.

It is demonstrated in the following example:

let multiply = new Function('a', 'b', 'return a * b');
console.log(multiply(2, 3)); // 6

In another example, you can see a function without arguments but with the function body:

let welcome = new Function('console.log("Welcome to w3cdoc")');
welcome(); // Welcome to w3cdoc

The most significant difference from other ways of creating a function is that it is created from a string passed at run time. In all the previous ways, developers were required to write the function code inside the script. But, with the help of the new Function, you can turn any string into a function. For instance, you can get a new Function from the server and execute it, like this:

let str = ...receive the code from a server dynamically...
let func = new Function(str);
func();

Particular cases require to use it. For example, when you get a code from a server or compile a function from a template. (特殊情况下需要使用它。例如,当您从服务器获取代码或从模板编译函数时。)

Closure

Closure (关闭)

As a rule, a function remembers where it was created in the particular property [[Environment]]. Hence, it references the Lexical Environment from where it’s made. Previously it was represented in the chapter Variable Scope. (通常,函数会记住它在特定属性[[Environment]]中的创建位置。因此,它从制造它的地方引用了词汇环境。此前,它在“变量范围”一章中有所体现。)

When you create a function by applying new Function, the [[Environment]] references not the current Lexical Environment, but also the global one. (通过应用新函数创建函数时, [[Environment]]引用的不是当前词法环境,而是全局词法环境。)

A function like this has no access to external variables:

function getFunc() {
 let value = "test";
 let func = new Function('alert(value)');
 return func;
}
getFunc()(); // error: value is not defined

Comparing it with normal behavior turns out:

function getFunc() {
 let value = "test";
 let func = function () {
   console.log(value);
 };
 return func;
}
getFunc()(); // "test", from the Lexical Environment of getFunc

The new Function may seem a little strange, but in practice, it can be beneficial. (新功能可能看起来有点奇怪,但在实践中,它可能是有益的。)

Let’s imagine that you intend to make a function from a string. At the time of writing the script, that function code is not known. But it can be found out during the execution. It might be received either from the server or from another source. An interaction between the new function and the main script is a necessity. (假设您打算从字符串创建一个函数。在编写脚本时,该函数代码未知。但它可以在执行过程中找到。它可能从服务器或其他来源接收。新函数和主脚本之间的交互是必要的。)

Before JavaScript is published to production, it is compressed by using a minifier. A minifier is a unique program that shortens code by deleting extra comments and spaces, as well as renaming the local variables into compact ones. (在将JavaScript发布到生产环境之前,它通过使用缩小符进行压缩。缩小器是一个独特的程序,它通过删除额外的注释和空格以及将局部变量重命名为紧凑变量来缩短代码。)

For example, when a function has let userName, the minifier may shorten it by replacing it with let a or another shorter variant. (例如,当函数具有let userName时,缩小器可以通过将其替换为let a或其他较短的变体来缩短它。)

Note that if new Function had access to the external variables, it might not be able to find the renamed userName. In case the new Function had access to the external variables, there would be no problems with minifiers. (请注意,如果新函数可以访问外部变量,它可能无法找到重命名的userName。如果新函数可以访问外部变量,则缩小器不会出现问题。)

For passing something to a function, created as new Function, its arguments should be used. (对于将某些内容传递给作为新函数创建的函数,应使用其参数。)



请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部