JavaScript function declarations and function expressions

Source: Internet
Author: User

There are two ways to define a function: One is a function declaration and the other is a function expression .

The grammar of the two

Syntax for function declarations:

function functionname (arg0, arg1, arg2) {        //body      }

Syntax for function expressions:

var functionname = function (arg0, arg1, arg2) {        //body  }

Because there is no identifier after the function keyword, the function created in this case is called an anonymous function (anonymous function, sometimes called a lambda). The Name property of the anonymous function is an empty string.

Two, the difference between the two

In ECMAScript, there are two most commonly used methods of creating function objects, that is, using function expressions or using function declarations. In this regard, the ECMAScript specification makes it clear that the function declaration must always have an identifier (Identifier), which is what we call the function name, and the function expression can be omitted.

An important feature of the function declaration is the function declaration promotion (functions declaration hoisting), which means that the function declaration is read before the code is executed. This means that the function declaration can be placed after the statement that called it.

Sayhi (); function Sayhi () {        alert ("Hi");      }

function expressions, like other expressions, must be assigned before they are used. The following code will cause an error.

Sayhi (); var sayhi = function () {        alert ("hi!");  }
Iii. conversion of the two

To improve development efficiency, developers use ifee(immediately-invoked function expression, instant execution method )

So why Iife?

    1. Traditional methods of wordy, definition and execution of separate writing;
    2. Traditional methods directly pollute the global namespace (objects in the browser global , such as window )
The traditional notation:
function foo () {...}     This is the definition, Declaration; the definition simply lets the interpreter know that it exists, but does not run. Foo ();                   This is the statement, Statement; The interpreter will run it when it encounters a statement.
IIFE的写法:
(function foo () {...})    Here is a deliberate line, can actually be linked with the following parentheses ();

The above is to make the function declaration into an expression, that is, the function declaration with a pair of () wrapped up

This is equivalent to:

var foo = function () {...};    This is not a definition, but an expression. Foo ();

In addition, there are many ways to change expressions. Learning while adding.

Reference:

JavaScript Advanced Programming

Execute function immediately

JavaScript function declarations and function expressions

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.