JavaScript uses function statements and expressions to define the difference between functions

Source: Internet
Author: User
Tags function definition

  This article mainly describes the differences between JavaScript functions defined by function statements and expressions. Need friends can come to the reference, I hope to help you.

Using JavaScript for many years, wrote countless functions, today but really understand the difference between the definitions of the two functions, is really a tragedy, write down this essay, to remind themselves to lay a good foundation, a lot of age, can not continue to be ignorant.   Usually we see the following two ways to define functions:  code as follows://Function statement functions fn (str) {  Console.log (str);  //Expression definition var fnx=function (str) {  Console.log (str+ ' from Fnx ');     used to use both of their own fingers-_-| | , today read the JS Foundation, finally solved the heart of their confusion:   Two methods have created a new function object, but the function declaration statement's function name is a variable name, the variable points to the function object, and through the Var declaration variable, Functions in a function definition statement are displayed in advance to the top of a script or function. So they are all visible within the entire script and function, but using the var expression to define the function, only the variable declaration is advanced, the variable initialization code is still in the original position, the function name and function body are created in advance , so we can use it before declaring it.     Code examples are as follows:   code is as follows:    console.log (typeof (FN)); function     FN (' abc '); ABC         Console.log (typeof (FNX)); Undefined       if (FNX)         FNX (' abc ');  //won't execute     else         Console.log (' fnx is undefined '); FNX is undefined      //Function statement     function fn (str)     {        Console.log (str);    };      //Expression definition     var fnx=function (str)     {        Console.log ( str+ ' from Fnx ');    };     The code is very simple, I hope that the same as I did not understand the difference between the students can gain.  
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.