JS defines functions in two ways: function declaration and function expression

Source: Internet
Author: User

function declaration

One of the important features 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, as follows:

1             sayhi ();               The function is declared after the call statement (Functions declaration hoisting) 2 function             Sayhi () {3                console.log ("Hello World"); 4             

  

function Expression

There are many ways of expressing it, the following is the most common one:

var(AGR1, arg2) {      + "  + arg2);  

As stated in the book, the function declares the statement that the function actually declares a variable F, then assigns a declared function object to the variable F, and the declaration of the variable has a feature that is raised to the top of all the code, so that functions declared by the function declaration statement can be called before the declaration.

As to why a function is also assigned to a variable, the function expression cannot be mentioned earlier. Let's analyze the following:

var s = function (Arg1, arg2) {};

  

Can be split into:

var s;s = function (Arg1, arg2) {}

  

It was var s declared in advance, but s = xxxx only when it was run here.

JS defines functions in two ways: function declaration and function expression

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.