JavaScript variable Scope full resolution _ Basics

Source: Internet
Author: User
Tags anonymous closure variable scope

The scope of a variable is the region in the program that defines the variable.
Let's take a look at an example:

/* Code 1 * *

var scope = "global";
function Checkscope () {
 var scope = ' local ';
 function Childcheck () {
  var scope = ' childlocal ';
  document.write (scope);
 function childundefined () {
  document.write (scope);
  var scope;
 }
 function Childoverride () {
  scope = ' childoverride ';
  document.write (scope);
 document.write (scope); Output "local"
 Childcheck ();   Output "Childlocal"
 childundefined ();  Output "undefined"
 childoverride ();  Output "Childoverride"
 document.write (scope);//Output "Childoverride"
}
Checkscope ();    Output "Local childlocal undefinedchildoverride childoverride"
document.write (scope);  Output "global"

Global scope and local scope
Global variables are global in scope and are defined everywhere in JavaScript, and variables declared within functions are local variables that are local in scope and are defined only within the body of the function. For the following output readers should not be surprised.
/* Code 2 * *

var scope = "global";
function Checkscope () {
 var scope = ' local ';
 document.write (scope);
Checkscope ();   Output "local"
document.write (scope);//Output "global"

Variables can be used in global variable scopes without the Var statement, but if the local variable is declared to be certain to use the Var statement, it is considered a reference to the global variable. Look at the following code:
/* Code 3 * *

var scope = "global";
function Checkscope () {
 scope = ' local ';
 document.write (scope);
Checkscope ();   Output "local"
document.write (scope);//output "local"

No block scope
JavaScript does not have a block-level scope, and the variables declared in the function are defined throughout the function. The following code may be quite unexpected to unfamiliar readers:
/* Code 4 * *

var scope = "global";
function Checkscope () {
 document.write (scope);//Statement 4.1
 var scope = "local";//Statement 4.2
 document.write ( scope);
}
Checkscope ();   Output "Undefinedlocal"

Because the statement 4.1 (var scope = "local";) the variable declared is valid throughout the Checkscope function scope, the statement 4.2 (document.write (scope); A local variable is referenced at the time of execution, and the local variable scope is not defined, so output "undefined". So a good programming habit is to put all the variable declarations together and place them at the beginning of the function.

Once you know the above, it's not confusing for the reader to look at code 1 again.
object's Property variable
the object's property variable is easier to understand, so look at the code below and the reader should not be confused.
/* Code 5 * *

var scope = "global";
var obj = new Object ();
Obj.scope = "Object";
Obj.checkscope = function () {
 var scope = ' Loacl ';
 document.write (scope);   Output "Loacl"
 document.write (this.scope);  Output "Object"
 document.write (Window.scope);//Output "global"
}
Obj.checkscope ();//Output "Loacl object Global

The so-called scope, which is the valid range of the variable in the code block. If you do not understand the JavaScript scope, it may be difficult to debug your code.

In a function, if you declare a variable with VAR, the scope of the variable is limited to that function, and code outside the function cannot access the variable. If you declare a function in the function, the internal function can also access the variable.

Conversely, if you declare a variable without using VAR, the scope of this variable is not limited to this function. The JavaScript engine will then check the global scope to see if the variable is defined. If the variable is not defined, it is defined as a global variable.

Functions can access variables in the same scope:

var foo = ' Hello ';

var SayHello = function () {
 console.log (foo);
};

SayHello ();   Logs ' Hello '
console.log (foo);//also logs ' Hello '

Code outside the scope of a variable cannot access the variable:

var SayHello = function () {
 var foo = ' Hello ';
 Console.log (foo);
};

SayHello ();   Logs ' Hello '
console.log (foo);//doesn ' t log anything

No variables with the same name in the scope have different values:

var foo = ' World ';

var SayHello = function () {
 var foo = ' Hello ';
 Console.log (foo);
};

SayHello ();   Logs ' Hello '
console.log (foo);//Logs ' world '

After the function is set, you can see the change in the value of the variable in the function:

var myfunction = function () {
 var foo = ' Hello ';

 var myfn = function () {
  console.log (foo);
 };

 foo = ' World ';

 return myfn;
};

var f = myfunction ();
f (); Logs ' world '--haha

Scopes can also traverse-closures

A self executing anonymous function
(function () {
 var baz = 1;
 var bim = function () {alert (baz);};
 bar = function () {alert (baz);};
}) ();

Console.log (BAZ); There is no access to Baz

Bar () outside the function ()///The bar is not used with Var
  //So bar is a global variable; however,
  //bar and Baz are defined in the same scope
  //So B AR can access Baz//
  In fact bar is a closure function

BIM ();//BIM scope is limited to anonymous functions,/
  /So this cannot be invoked here.

Comprehensive

The so-called scope, which is the valid range of the variable in the code block. If you do not understand the JavaScript scope, it may be difficult to debug your code.

In a function, if you declare a variable with VAR, the scope of the variable is limited to that function, and code outside the function cannot access the variable. If you declare a function in the function, the internal function can also access the variable.

Conversely, if you declare a variable without using VAR, the scope of this variable is not limited to this function. The JavaScript engine will then check the global scope to see if the variable is defined. If the variable is not defined, it is defined as a global variable.

Functions can access variables in the same scope:

var foo = ' Hello ';

var SayHello = function () {
 console.log (foo);
};

SayHello ();   Logs ' Hello '
console.log (foo);//also logs ' Hello '

Code outside the scope of a variable cannot access the variable:

var SayHello = function () {
 var foo = ' Hello ';
 Console.log (foo);
};

SayHello ();   Logs ' Hello '
console.log (foo);//doesn ' t log anything

No variables with the same name in the scope have different values:

var foo = ' World ';

var SayHello = function () {
 var foo = ' Hello ';
 Console.log (foo);
};

SayHello ();   Logs ' Hello '
console.log (foo);//Logs ' world '

After the function is set, you can see the change in the value of the variable in the function:

var myfunction = function () {
 var foo = ' Hello ';

 var myfn = function () {
  console.log (foo);
 };

 foo = ' World ';

 return myfn;
};

var f = myfunction ();
f (); Logs ' world '--haha

Scopes can also traverse-closures

A self executing anonymous function
(function () {
 var baz = 1;
 var bim = function () {alert (baz);};
 bar = function () {alert (baz);};
}) ();

Console.log (BAZ); There is no access to Baz

Bar () outside the function ()///The bar is not used with Var
  //So bar is a global variable; however,
  //bar and Baz are defined in the same scope
  //So B AR can access Baz//
  In fact bar is a closure function

BIM ();//BIM scope is limited to anonymous functions,/
  /So this cannot be invoked here.

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.