JS definition function

Source: Internet
Author: User

There are three ways to define a function: 1,function fun () This is called a function declaration; 2,var fun=function () is called a function expression; 3,var fun=new function () is also called a function expression.
In the same execution environment, the function declaration is promoted to the front of the code, no matter where it is, even if it is preceded by a function expression, this is the JS rule.

Examples:

function Fun () {                        alert (0);                       } Fun (), var fun = function () {                                 alert (1);                                } Fun (), var fun =function () {                                 alert (2);                               } Fun (), function Fun () {                        alert (3);                       } Fun ();

Answer: 3122


The first thing to do is definitely a fun (), which points to the last function fun (). It then executes a second fun (), which points to the first var fun = function (). Then execute the third fun (), the second var fun = function () overrides the first, because JS has no overloaded method, so this is the process of running.

The equivalent of the code becomes this:

function Fun () {                        alert (0);                       } function Fun () {                        alert (3);                       } Fun (), var fun = function () {                                 alert (1);                                } Fun (), var fun =function () {                                 alert (2);                               } Fun (), fun ();

JS definition function

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.