Different _javascript techniques for function expressions and function declarations and function declarations and function expressions in JavaScript

Source: Internet
Author: User

function expressions and Function declarations

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.

 function foo () {}//declaration because it is part of the program
 var bar = function foo () {};//expression, because it is part of the assignment expression
 new function bar () {};//Expression, because It is the new expression
 (function () {
  function bar () {}//declaration, because it is part of the function Body
 }) ();

There is also a function expression that is not very common, which is enclosed in parentheses (function foo () {}), because it is an expression because parentheses () is a grouping operator whose interior can only contain expressions, let's look at a few examples:

function foo () {}//Functions Declaration
(function foo () {}); Function expression: Included in Group operations characters

named function expression

The reference to a named function expression, of course, is that it has to have a name, the preceding example var bar = function foo () {}; is a valid named function expression, but one thing to remember: The name is valid only in the newly defined function scope, Because the specification stipulates that identifiers cannot be valid within the scope of the perimeter:

 var f = function foo () {return
  typeof foo;//foo is valid within scope
 };
 Foo is used externally for invisible
 Console.log (typeof foo);//"Undefined"
 Console.log (f ());//"function"
var f = function Foo () {return
foo;//foo is valid within scope
};
Foo is used externally for invisible
Console.log (typeof foo);//"Undefined"
Console.log (f () ==f);//"function"
Console.log (f.name);//foo

So, what's the use of a named function expression, if that's required? Why do you have to name it?

As we said at the beginning: Give it a name is to make debugging process more convenient, because in debugging, if each item in the call stack has its own name to describe, then the debugging process is too cool, feel different.

The difference between a function declaration and a function expression in Ps:js

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?

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.

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.