function declarations and function expressions

Source: Internet
Author: User

The two look very much alike.

Functions declaration: Function Function name () {}

Functional Expressions: Function name (writable and not writable) () {}: Named function expression/anonymous function expression

For a few examples:

function aaa () {}: Functions declaration

var a=function aaa () {}: named function expression

var a = function () {}: an anonymous function expression;

(function aaa () {}): expression

Difference:

1. Function expressions can be directly followed by parentheses, and function declarations do not

function aaa () {alert (1);} () (Error)

var a=function aaa () {alert (1);} () (Run directly)

2. function declaration can be resolved in advance

if (true) {function aaa () {alert (1);}}

else{function aaa () {alert (2);}};

AAA (); The execution result is indeterminate, possibly 1, possibly 2, because the function declaration is pre-parsed and AAA is parsed before the IF

if (true) {var a=function aaa () {alert (1);}}

else{var a= function aaa () {alert (2);}};

A (); The execution result is 1 first executed if statement.

3. The difference between anonymous and named functions

var a=function aaa () {alert (1);}

A (); All browsers support

AAA (), non IE error but put AAA in the function can be. IE will parse it into a () and AAA ();

(function aaa () {alert (1)})

AAA () Non IE error.

function declarations and function expressions

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.