In-depth analysis of JS Function () constructor and jsfunction

Source: Internet
Author: User

In-depth analysis of JS Function () constructor and jsfunction

Function objects in js are fascinating, but they are often confusing because they are too flexible.

Let's take a look at the code snippet.

var scope="global";function constructFunction(){var scope="local";return new Function(" return scope");}constructFunction()(); function constructFunction2(){var scope="local";return function(){return scope;}}constructFunction2()();

What are your first feelings? "Local "??, If this is the case, you need to take a good look. ConstructFunction2 () It is easy to know the answer to the closure is "local". I will not elaborate on it here. Next I will focus on the situation of constructFunction.

Function () constructor is used here. Although Function () constructor is not very common, it is necessary to understand it.

Whether using a function Definition Statement or a function's direct expression, the function definition must use the function () keyword. A single Function can also be defined by a Function () constructor, for example:

 var f=new Function("x","y","return x*y");

The actual effect of this line is equivalent to the following line of code.

var f=function(x,y){x*y};

Function () constructor can input any number of string real parameters. The last real parameter represents the Function body. It can contain any Javascript statement, separated by semicolons. All other arguments passed in to the constructor are strings that specify the name of the function. If the defined function does not contain any parameters, you only need to input a string function body to the constructor.

Pay special attention to the following points for Function () constructor:

1. Function () constructor allows JavaScript to dynamically create and compile functions at runtime.

2. Each call to the Function () constructor will parse the Function body and create a new Function object. If the constructor is executed in a loop or multiple calls, the execution efficiency will be affected. In contrast, nested functions and Function Definition expressions in a loop do not re-compile each execution.

2. the last point is that the Function () constructor is very important, that is, the Function it creates does not use the lexical scope. On the contrary, compilation of the function body code is always performed on top-level functions. After reading this, the above function constructFunction (); should return "global" easily understood?

The above is the JS Function () constructor introduced by xiaobian. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.