Elevation of scopes and functions and variable declarations in JavaScript

Source: Internet
Author: User

var foo = 1;
function Bar () {
if (!foo) {
var foo = 10;
}
Alert (foo);
}
Bar ();//Explanation: JS does not have block-level scope, if switch while etc. will not have block-level scope

If you are surprised that the value of Foo is actually "10", take a look at the following example:

var a = 1;
Function B () {
A = 10;
Return
function A () {}
}
b ();
alert (a);
Discover that the browser pops up alert (1).
Explanation: The 1th is that the promotion of a function declaration promotes the implementation of the function to the top of the scope, and the function B above is equivalent to
Function B () {
function A () {}
A = 10;
Return

}
The 2nd is: In this case, we define a function in the B function, the scope of a is within the B function rather than the global, this is the assignment of a to replace the A function, the scope does not change, the equivalent of using VAR to define a variable of a ; So when I output a in the outside of the B function, we only go to the global A;
var a = 1;
Function B () {
A = 10;
Return
function A () {}
}
b ();
alert (a);//Error A is undefined

Also: if the A function here is not defined by the function declaration, but the variable is defined, the variable is promoted and the function implementation does not improve: The following:
var a = 1;
Function B () {
A = 10;
Return
A=function () {}//A is a global variable here
}
b ();
alert (a);//10
If that's the case,
var a = 1;
Function B () {
A = 10;
Return
var a=function () {}//A is a local variable here
}
b ();
alert (a);//1
Equivalent to this:
var a = 1;
Function B () {
var A;
A = 10;
Return
A=function () {}//A is a local variable here
}
b ();
alert (a);//1
It can be found that the portion of the assignment in the declaration statement is not promoted, and only the name is promoted. Two ways to declare a function:
 function test ()  {  
    foo (); // typeerror  "foo  is not a function " 
    bar (); // " This will  run! "  
    var foo = function  ()  { // function  expression assigned to local variable  ' foo '  
         alert ("This won ' t run!");  
    } 
    function bar ()  { //  function declaration, given the name  ' Bar '  
         alert ("this will run!");  
    } 

Test ();
In this example, only the function declaration containing the body of the function is promoted. Foo is declared as elevated, but the function body is assigned in execution. The above is the basic concept of the timing of the declaration, and it does not seem complicated at all.

Name resolution order (in JavaScript , names resolved in sequence)

There are 4 steps in the name resolution order, in general, if a name is already defined, it is not overwritten by another property with the same name. This also means that a function declaration takes precedence over a variable declaration. An assignment operation that is not a name is not executed, except that the declaration part is ignored. Some exceptions:
    • The native variable arguments is maverick and contains the arguments passed to the function. If you customize the arguments parameter to be named, the creation of the native object will be blocked arguments . So do not arguments use parameters as names.
    • Random use of this identifiers can cause syntax errors.
    • If more than one parameter has the same name, the last parameter will take precedence over the previous one, and this parameter is undefined immediately.


Named functions Expressions (function-named expressions)

You can name a function through a function expression, which looks like a function declaration, but it's not. Previous section of code:
Foo (); TypeError "Foo is not a function"
Bar (); Valid
Baz (); TypeError "Baz is not a function"
Spam (); Referenceerror "spam is not defined"

var foo = function () {}; anonymous function expression (' foo ' gets hoisted)
function bar () {}; function declaration (' Bar ' and the function body get hoisted)
var baz = function spam () {}; Named function expression (only ' Baz ' gets hoisted)

Foo (); Valid
Bar (); Valid
Baz (); Valid

Elevation of scope and function and variable declarations in JavaScript

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.