The difference between a JavaScript function declaration and a function expression

Source: Internet
Author: User


When defining a function, we typically use the following two methods:

The function declaration in JS refers to the following form:


function functionname () {

}

This way to declare a function, and a function expression is like an expression to declare a function, such as:


var functionname = function () {
}

Probably many friends in see these two kinds of writing will produce the doubt, these two kind of writing similar, in the application seemingly also all is feasible, then they have what difference?

Use a function declaration definition:

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

Use function expressions to define:

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

The calling method is the same:

If "1+1" equals several:

Alert (sum (1, 1));

But there is a difference between the two methods. When the parser loads data into the execution environment, it does not discriminate between function declarations and function expressions. The parser takes the lead in reading the function declaration and making it available before executing any code, and the function expression must wait until the parser executes to the line of code it is in before it is actually interpreted.

In fact, the JS parser does not treat the function declaration and function expression equally. For function declarations, the JS parser will be read first, make sure that the declaration has been resolved before all the code executes, and that the function expression, like a variable that defines other basic types, is parsed only as it executes to a sentence, so in practice they are still different, specifically, When you use the form of a function declaration to define a function, you can write the call statement before the function declaration, and the latter will report an error.

Example:

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

The code above can be executed normally. Because before the code executes, the parser has read and added the function declaration to the execution environment through a procedure named function declaration elevation. When the code is evaluated, the JavaScript engine declares the functions for the first time and places them at the top of the source tree. So, even if the code that declares the function is placed behind the code that calls it, the JavaScript engine can also elevate the function declaration to the top. If the above function declaration is changed to a function expression, as shown in the following example, an error is caused during execution.

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

The above code causes an error during execution because the function is in an initialization statement, not a function declaration. In other words, the variable sum does not hold a reference to the function until the statement in which it is executed, and the first line of code has generated an error and is not executed to the next line.


In general, the syntax of a function declaration and a function expression is actually equivalent to the difference between the variable access function and the

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.