JavaScript variable Scope

Source: Internet
Author: User
Tags define function variable scope

All variables have global scope, local variables have local scope (note that the parameters of the function are also local variables)

1. In the function body, the local variable takes precedence over the global variable with the same name.

My understanding is that when you define both local and global variables with the same name, the function body returns the value of the local variable.

For example:

var scope= "I am a global variable"; function Checkscope () {    var scope= "I am a local variable";     Console.log (scope);} Checkscope ();

Output: I am a local variable

It is important to note that VAR can be omitted when declaring a global variable (in the function body if you do not use Var to declare a local variable then the variable will be a global variable, but it must be declared when declaring a local variable, otherwise it is easy to disturb the scope of the global variable).

Scope= "I am a global variable"; function Checkscope () {     scope= "I am a local variable";     Console.log (scope);} Checkscope (); Console.log (scope);

Output: I am a local variable

I'm a local variable.

The second output should be a global variable, but because the function body declares a global variable with the same name, the scope of the global variable is disturbed.

The following is a case of local scope nesting

var scope= "I am a global variable"; function Checkscope () {    var scope= "I am a local variable"     ; function nested () {         var scope= "nested local variable";          Console.log (scope);     }     return nested ();} Checkscope ();

Output: Nested local variables
This can be helpful in understanding the closure that you learn later.

2. function scope and function declaration in advance

(1) To be clear is that there is no block-level scope in JavaScript, instead of the function scope

(2) Variables are defined in the body of the function in which they are declared and in any function within which the body of the function is nested.

(3) JavaScript function scope means that all variables declared within a function are always visible inside the function body.

What does that mean? It must have been magical to learn other languages. In C, variables are defined and used, and are not needed here. As long as the variable you are using has been defined in the function body, using this variable can be anywhere within the body of the function. This is the JS declaration in advance of the magic, meaning that JS will automatically refer to the top of the variable declaration, for example:

var scope= "I am a global variable"; function f () {     console.log (scope);      var scope= "I am a local variable";      Console.log (scope);} f ();

Output: undefined
I'm a local variable.
If you do not know the declaration in advance you will feel that the first output will be "I am a global variable" but in fact, at the top of the function and the declaration ahead of time, the local variable scope has been defined but not assigned, so the undefined

3. Variables as attributes

The JS global variable is a property of the global object (note that the global variable declared with VAR indicates that the property created is not configurable to be deleted by the delete operator), or that the global object can be referenced with the This keyword, which can be configured to be deleted.

Ar truevar=1; Fakevar=2; this. fakevar2=3; Console.log (Delete  fakevar); Console.log (Delete  Truevar); Console.log (Deletethis. fakevar2);

Output

True
False
True

4. Scope Chain

Understand two points:

1. Global variables are always defined in the program

2 Local variables are always defined in the function body and within the nested function body.

See an easy-to-understand method in a book:

Consider a local variable as a property of the custom implemented object. Then each function or global function has a scope of action, which is a list of objects or linked lists. When looking for a variable x, from the first object in the chain to find, with this attribute is used, do not continue to look down, the entire scope of the chain does not throw a reference exception.

In the top-level code of JS, the chain of action consists of:

1: Objects that define function parameters and local variables

2: Global Object

Scope chains for nested functions:

1. Scope chain saved when defining an outer function

2. When the function is called, the new object is created to store the local variable, then the object is saved to the scope chain mentioned in 1, and then a new scope chain is created that represents the function call.

3. For nested functions, each time an external function is called, the inner function is redefined again, creating a new scope chain.

JavaScript variable Scope

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.