JS Scope just a few words

Source: Internet
Author: User
Tags throw exception

The scope of JavaScript has always been the front-end development of the more difficult to understand the knowledge point, for the scope of JavaScript mainly remember a few words, go all over the world are not afraid of ...

One, "no block-level scope in JavaScript"

There is a block-level scope in Java or C #, which is: curly braces are also a scope.

 Public Static void Main () {    if(1==1) {        = "Seven";    }    SYSTEM.OUT.PRINTLN (name);} // Error
 Public Static void Main () {    if(1= =1)        {string"Seven ";    }    Console.WriteLine (name);} // Error
C #

No block-level scopes in the JavaScript language

1234567 function  main () {      if (1==1) {           var  name =  ' seven '           console.log (name); //output: Seven

Add: The title adds double quotes because the LET keyword is introduced in JavaScript6 to specify that the variable belongs to a block-level scope.

Second, JavaScript uses function scope

In JavaScript each function acts as a scope, and the variables in the internal scope cannot be accessed externally.

function Main () {    var innervalue = ' Seven ';} Main (); Console.log (Innervalue); Error: Uncaught referenceerror:innervalue is not defined
Third, the scope chain of JavaScript

Because each function in JavaScript acts as a scope, the scope chain appears if function nesting functions occur.

1234567891011 xo = ‘alex‘; functionFunc(){    varxo = "seven";    functioninner(){        varxo = ‘alvin‘;        console.log(xo);    }    inner();}Func();

As shown above, there are three scopes of scope chain, if the scope chain, then the search for variables will appear in order, for the above example:

When executing Console.log (XO), its search order is based on the scope of the chain from the inside to the outside of the priority, if the inner layer does not step up to find, until the throw exception is not found.

Iv. The scope chain of JavaScript was created before execution

The scope of JavaScript is created before it is executed, and it is only necessary to follow the scope chain to find out when to execute it later.

Example one:

1234567891011121314 xo = ‘alex‘; functionFunc(){    varxo = "seven";    functioninner(){        console.log(xo);    }    returninner;}varret = Func();ret();// 输出结果: seven

The above code, before the function is called, the scope chain already exists:

    • Global scope, Func function scope, inner function scope

When performing ret (); , since its surrogate refers to the inner function, the scope chain of this function has been defined before execution: global scope, Func function scope, and inner function scope, so, in the execution of RET (); , the variable is searched based on the scope chain that already exists.

Example two:

123456789101112131415 xo = ‘alex‘;functionFunc(){    varxo = "eirc";    functioninner(){        console.log(xo);    }    xo = ‘seven‘;    returninner;}varret = Func();ret();// 输出结果: seven

The code above is the same as example one, and it also emphasizes that the scope chain already exists before the function is called:

    • Global scope, Func function scope, inner function scope

Different when executing var ret = Func (); , the value of the XO variable in the func scope has been reset from "Eric" to "seven", so the RET () is then executed; , you can only find "seven".

Example three:

1234567891011121314 xo = ‘alex‘;<br>functionBar(){    console.log(xo);}functionFunc(){    varxo = "seven";        returnBar;}varret = Func();ret();// 输出结果: alex

The code above has created two scope chains before the function is executed:

    • global scope, bar function scope
    • Global scope, Func function scope

When performing ret (); , the bar function is referred to as RET, and the scope chain of the bar function already exists: global scope and bar function scope, so execution will be based on the existing scope chain to find.

V. Declaration of Advance

In JavaScript, if you do not create a variable and go directly to it, the error is:

12 console.log(xxoo);// 报错:Uncaught ReferenceError: xxoo is not defined

If you create a value without assigning a value in JavaScript, the value is undefined, such as:

123 varxxoo;console.log(xxoo);// 输出:undefined

Within the function, if you write this:

1234567 functionFoo(){    console.log(xo);    var xo = ‘seven‘;}Foo();// 输出:undefined

The above code, not error, but output undefined, the reason is: the JavaScript function before it is executed, the variables are all declared, without assigning value. Therefore, the equivalent of the above example, the function in "precompiled" when the Var XO has been executed, so the above code output is undefined.

JS Scope just a few words

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.