Differences between javascript function declaration and function expressions _ javascript tips-js tutorial

Source: Internet
Author: User
Function declarations and function expressions in javascript are frequently used, and many friends may not know the differences between them. I would like to introduce them in detail and hope they will help you the same way, first run the Code:

The Code is as follows:


Script
Var f = function g (){
Return 1;
};
If (false ){
F = function g (){
Return 2;
};
}
Alert (g (); // 2
Script


Throwing this code into IE 6 is totally different from chorme.

Here, output 2 is the effect in ie6. g is not defined if it appears in chorme.

This is also a JScript bug.

It is obvious that only the g function expression is defined here. Included in the if Condition Statement, only the function expression is defined and the function is not declared.

Therefore, direct access will certainly result in errors.

So what is Declaration and function expression?

In ECMAScript, the two most common methods for creating a function are function expressions and function declarations. The difference between the two methods is a bit dizzy, because the ECMA specification only makes it clear: the function declaration must contain an Identifier (the name of a function that is commonly used), while the function expression can omit this Identifier:
Function declaration:
Function Name (parameter: OPTIONAL) {function body}
Function expression:
Function Name (optional) (parameter: OPTIONAL) {function body}

Therefore, we can see that if the function name is not declared, it must be an expression. If the function name is declared, how can we determine whether it is a function declaration or a function expression? ECMAScript is distinguished by context. If function foo () {} is a part of the value assignment expression, it is a function expression. If function foo () {} is contained in a function, or at the top of the program, it is a function declaration.

Another function expression is not very common, that is, function foo () {}) enclosed by brackets. It is the reason for the expression because parentheses () are a grouping operator, it can only contain expressions.

You may think that when using eval to execute JSON, The JSON string is usually included in a parentheses: eval ('+ json + ')'), the reason for doing so is that the grouping operator, that is, this pair of parentheses, will force the parser to parse the JSON curly braces into expressions rather than code blocks.
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.