A description of the difference between JavaScript function declaration and function Expression _javascript tips

Source: Internet
Author: User
Or the same, first code:
Copy Code code 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 and chorme is a completely different two-way effect.

Output 2 Here is the effect in IE6, if G is not defined in Chorme.

This is also a JScript bug.

It is obvious here that only the function expression of g is defined here. Included in the conditional statement of if, only the function expression is defined, not the function is declared.

So direct access is definitely going to go wrong.

So what is a declaration and what is a function expression?

In ECMAScript, the two most commonly used methods of creating functions are function expressions and function declarations, and the difference between the two is a bit faint, because the ECMA specification is only a bit clear: the function declaration must have an identifier (Identifier) (the name of the function that everyone often says). And a function expression can omit this designator:
function declaration:
function function name (parameter: optional) {Functional Body}
function expression:
function function name (optional) (parameter: optional) {Functional Body}

So, as you can see, if you don't declare the function name, it's definitely an expression, but if you declare the function name, how do you decide whether it's a function declaration or a function expression? ECMAScript is differentiated by context, and if function foo () {} is part of an assignment expression, it is a function expression, if function foo () {} is contained in a function body, or at the top of the program, So it's a function declaration.

There is also a function expression that is less common and is enclosed in parentheses (function foo () {}) and is an expression because parentheses () is a grouping operator whose interior can only contain expressions.

You might think that when you use Eval for JSON, the JSON string is usually enclosed in parentheses: eval (' (' + JSON + ') '), because the grouping operator, which is the pair of parentheses, Lets the parser force the curly braces of the JSON to parse into an expression instead of a block of code.
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.