A brief analysis of several differences in JavaScript function definition

Source: Internet
Author: User
Tags anonymous constructor function definition

  This article is mainly about the JavaScript function definition of several differences in a detailed summary of the introduction, the need for friends can come to the reference, I hope to help you.

Definition of JavaScript function   1: Invoke keyword function to construct, such as: function distance (x1,x2,y1,y2) {var dx=x2-x1; var dy=y2-y1; return Math.sqr T (Dx*dx+dy*dy); }   2: Use function () constructor var f=new function* "x", "Y", "return X*y"); This line of code creates a new function that is essentially equivalent to a function that you are familiar with syntax-defined:   function f (x,y) {return x*y}   Functino () constructor can accept any number of string arguments. Its last argument is the body of the function, which can contain any JavaScript statements, separated by semicolons. Other parameters are strings used to describe the formal parameter names that the function is to define. If you define a function that has no parameters, you can simply pass a string (i.e. the body of the function) to the constructor.   Note that there is not one parameter passed to the constructor function () that describes the name of the function that it is to create. An unnamed function created with a function () constructor is sometimes called an anonymous function.   You may be very interested to know what function () constructor is used for. Why not use Function statements to define all the functions? The reason is that the function () constructor allows us to dynamically build and compile functions that do not limit us to functions that are precompiled in function statements. The downside effect of doing this is that the function () constructor compiles every time you call one of the functions. Therefore, we should not call this constructor frequently in a loop body or in a function that is often used.   Another reason to use the function () constructor is that it can define a function as part of a JavaScript expression, rather than defining it as a statement, in which case it is even more elegant to use it.   3: The direct quantity function is an expression that can define anonymous functions. The syntax of the direct volume of a function is very similar to a function statement, except that it is used as an expression, not as a statement, and does not need to specify a function name. The following three lines of code define three essentially identical functions using the function () statement, the Funciont () constructor, and the direct amount of the function:   function f (x) {RetuRN X*x}; var f=new Function ("x", "Return x*x;"); var f=function (x) {Reurn x*x};   Although the direct amount of a function creates an unnamed function, its syntax also specifies that it can specify a function name, which is useful when writing recursive functions that call itself. For example: Var f=function fact (x) {if (x<=1) return 1;else return X*fact (x-1); The code above defines an unnamed function, and its reference is stored in the variable F. It doesn't really create a function called fact (), it just allows the function body to use that name to refer to itself. Note, however, that the direct amount of this named function is not properly implemented in the previous version of JavaScript1.5. The use of the   function as a direct quantity is very similar to the method of creating functions using function () constructors. Since they are all created by JavaScript expressions rather than by statements, the way they are used is more flexible, especially for functions that are used only once and do not need to be named. For example, a function specified using a function direct quantity expression can be stored in a variable, passed to another function, or even directly called:   a[0]=function (x) {return x*x;};/ /define a function and save it a.sort (function (a,b) {return a-b;}); /define a function; pass it to another function Var tensquared= (function (x) {return x*x;}) (10); Like the   function () constructor, the direct amount of the functions creates an unnamed function and does not automatically store the function in the property. However, there is an important advantage to the direct quantity of a function, compared to the function () constructor. The body of a function created by the function () constructor must be described with a string, and it is clumsy to express a long and complex function in this way. But the body of the direct volume of the function uses the standard JavaScript syntax. Moreover, the direct amount of the function is parsed only once, while the JavaScript code passed as a string to the function () constructor is only parsed once and compiled once each time the constructor is invoked.   in JavaScript1.1, you can use the constructor function function () to define functions, and in JavaScript1.2 and subsequent versions, you can also use the direct quantities of functions to construct functions. You should be aware that between these two methodsThe important difference.   First, the constructor function () allows JavaScript code to be created and compiled dynamically at run time. But the direct amount of the function is a static part of the function structure, just like a function statement.   Secondly, as the inevitable result of the first difference, the function body is parsed each time the constructor function function () is invoked and a new East Han number object is created. This method is very inefficient if a call to a constructor appears in a loop, or in a function that is often called. In another respect, the function's direct volume or nested functions appearing in loops and functions are not recompiled at every call, and a new function object is not created whenever a direct amount of the function is encountered. The 3rd difference between the number of   function () constructors and functions is that functions created using constructor function () do not use lexical scopes, instead they are always compiled as top-level functions, as explained in the following code:   var y= " Global "; function constructfunction () {var y= ' local '; return new Function (' Return y ');//Do not capture localized scope. //This line of code will display "global" because functions returned by the function () constructor do not use local scopes. If you use a direct amount of a function, this line of code may display "local". Alert (Constructfunction ());  

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.