function Direct volume definition of javascript

Source: Internet
Author: User

In JavaScript, a function is allowed to be defined by a direct amount. In general, when we define a function, the most common way is to define it via a function statement, for example:

function sum (A, b) {
return a+b;
}

In this way, the SUM function can be arbitrarily called in the scope where it is located. In addition, the way a function is defined can be defined by a direct volume, in another way. For example, in the example above, we can use another way:

var sum=function (A, b) {
return a+b;
}

The code above is defined by the direct amount of the function. The direct amount of a function is an expression that defines an anonymous function. The syntax of a function's direct volume is very similar to a functions statement, except that it is used as part of an expression and not as a statement. In the function direct volume definition, the right function defines an anonymous function and passes a reference to the anonymous function to the left of the expression.

On the definition of function direct volume, we need to explain several points:

First, the function defined by the direct volume definition is an anonymous function, although the function is assigned to the variable name on the left by an expression-assignment statement, but the left-hand variable is assigned a reference to the anonymous function.

Second, because of the way in which the function is defined directly (code 2), functions are actually treated as an expression, so the variable on the left side of the expression must be executed after that statement to refer to the anonymous function on the right, that is, before the statement executes, The scope only knows that a variable is sum, but the type is unknown. In contrast, a regular function statement (code 1) is "preprocessed" before JavaScript executes, and in its scope, sum is recognized as a function from the beginning. A very simple example:

alert (a);//Eject undefined
alert (b);//Popup b.tostring () after the result
var a=function () {return 1;}
Function B () {return 1;}
alert (a);//Popup a.tostring () After the result

In the code above, the first alert (a), because of "preprocessing", the scope is already known to have a variable a, but because it has not yet been executed to line 3rd, so it is Undefind, and b at the beginning, you already know its type is a function. Therefore, when alert (b) is executed, it executes the b.tostring () method directly, although its definition is below the execution statement.

Third, although the function direct volume creates an anonymous function, its syntax also specifies that it can specify the function name. Only this function name is very effective when writing a recursive function that calls itself. To illustrate this problem, look at the following code:

var sum=function Temp (n) {return n==1?1:n*temp (n-1);}
Alert (SUM (5));//120
Alert (Temp (5));//error

The above code sum is a factorial (n!) that calculates n Operation, we can see that the definition of sum is defined by the direct amount of the function. But there is a problem here, that is, it has a function name: Temp. To be exact, temp is not a function name because, as I said earlier, the function on the right side of the expression is an anonymous function, so temp cannot be a function name (not to mention anonymous functions). )。 But why is there no error in defining this syntax? Because JavaScript allows you to specify its function name, the function name is not a true function name, it is used for recursive invocation of itself. (The function body is actually a recursive function). I generally understand that temp is the function name within the scope of an anonymous function, and it can call itself this way. Therefore, when executing to alert (temp (5)), the browser makes an error and tells us that temp is undefined.

It should be noted, however, that in earlier versions of JavaScript there was no correct implementation of the direct amount of this named function. In these versions of IE678, the JavaScript version is less than 1.5, so it has not been implemented correctly in this way. As a result, when you execute to alert (temp (5)), it is also the same as displaying and executing sum (5). As a result, temp and sum are all assigned values. This problem has been fixed in IE9 and higher versions.

function Direct volume definition of javascript

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.