JavaScript advanced series-function declaration and expressions

Source: Internet
Author: User
A function is a first-class object in JavaScript, which means that a function can be passed like other values. A common usage is to pass an anonymous function as a callback function to an asynchronous function.
  • Function Declaration

  • Function value assignment expression

  • Value assignment expression of the namefunction


A function is a first-class object in JavaScript, which means that a function can be passed like other values. A common usage is to pass an anonymous function as a callback function to an asynchronous function.

Function Declaration

function foo() {}

The above method will be parsed (hoisted) before execution, so it exists in any part of the current context, even if it is called on the Function Definition body.

Foo (); // normal operation, because foo has been created before the code runs function foo (){

Function value assignment expression

var foo = function() {};

In this example, an anonymous function is assigned to the variable foo.

Foo; // 'undefined' foo (); // error: TypeErrorvar foo = function (){};

Var defines a declaration statement. the parsing of the variable foo is before the code runs. Therefore, the foo variable has been defined during code runtime.

However, since the value assignment statement is only executed at runtime, the foo value is undefined by default before the corresponding code is executed.

Value assignment expression of the namefunction

Another special case is to assign a name function to a variable.

Var foo = function bar () {bar (); // normal operation} bar (); // error: ReferenceError

Bar function declaration is not visible, because we have assigned the function to foo; however, it is still visible inside bar. This is because of the JavaScript naming process, and the function name is always visible in the function.

Note: In IE8 and IE8 or earlier versions, the browser bar is also visible externally because the browser has incorrectly parsed the name function assignment expression and parsed it into two functions: foo and bar.


The above is the JavaScript advanced series-function declaration and expression content. For more information, see the PHP Chinese website (www.php1.cn )!

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.