JavaScript mode-function promotion (note)

Source: Internet
Author: User

function Promotion:

For all variables, no matter where the function body is declared, it will be promoted to the top of the function in the background. The same is true for functions, because the function is simply the object assigned to the variable. When a function declaration is used, the function definition is also promoted, not just the function declaration.

function foo () {   alert (' Global foo ');} function Bar () {   alert (' Global bar ');} function Hoistme () {   console.log (typeof foo),//function variable foo, and implementation are all promoted Console.log (typeof Bar); Undefined bar variable is not promoted by the promoted function foo (); Global foo bar (); Uncaught Typeerror:bar is not a function function foo () {console.log (' Global foo ');} var bar =function( {console.log (' local foo ');};} Hoistme ();

Foo () and bar in the Hoistme () function move to the top, overwriting the Foo and bar functions

Scope:

Only function scopes exist in JavaScript. Any variable defined inside the function with the VAR keyword is a local variable and is not visible outside the function. If the IF condition statement or a variable is defined using the var keyword in a for, while loop, this does not mean that the variable is a local variable for if or for. It is only a local variable for the wrapper function, and if there is no wrapper function, it becomes a global function.

// the I defined here for is a local variable. Scope only in part function part () {    for (var i=0;i<10;i++) {         // ..........    }  }// Here is all the variables defined here for.  for (var i=0;i<10;i++) {       //... ..   

Function named property:

function fn () {}//Functions Declaration

var fn=function () {};//function expression

var fn=function fn () {};//function-named expression

The Name property is useful when debugging in the debugger, and when the debugger needs to show an error in the function, it can detect if the Name property exists and use it as an indicator.

The Name property can also be used to recursively invoke the same function within itself.

JavaScript mode-function promotion (note)

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.