[Label] [JavaScript] [The Defined Guide of JavaScript] variable scope

Source: Internet
Author: User
Tags variable scope

Scope of the variable

A variable's scope (scope) is the area in the program that defines the variable.

Global variables scope (scope) is global, that is, in JavaScript code, it is defined everywhere.
Variables declared within a function are defined only in the body of the function, which are local variables, and the scope is local.
The parameters of the function are also local variables, which are defined only in the body of the function.
Inside a function body, a local variable has a higher precedence than a local variable with the same name.

This global variable is effectively hidden if the name of the parameter declaration for a local variable or function is the same as the name of a global variable.

For example, the following code will output the word "local scope":
var scope = ' global scope '; Declare global variable
function Checkscope () {
var scope = ' local scope '; Declare a local variable with the same name
document.write (scope); Use the local variable. Not the global One
}

Checkscope (); Prints "Local"
Note: You can write code in the global scope without using the VAR statement, but when declaring a local variable, be sure to use the Var statement.

No block-level scopes
JavaScript does not have a block-level scope, and all variables declared in the function, regardless of where they are declared, are in the entire function.
Defined by the.

The scope of the variables I, j, and K are the same in the code below, and three of them are defined in the entire function body.
Funtion Test (o) {
var i = 0; I is defined throughout function
if (typeof o = = "Object") {//If block
var j = 0; J is defined everywhere, not just block
for (var k = 0; k < k++) {//k are everywhere, not just loop
document.write (k); K is still defined:prints 10
}
}
document.write (j); J is defined, and may isn't initialized
}

Example-2

var scope = ' Global ';
function f () {
alert (scope); Displayed "Undefine", not "global"
var scope = "local"; Variable initialized here, but defined everywhere
alert (scope); Displays "local"
}
Due to the limitation of this scope rule, the first alert output is not "global".
Local variables are defined throughout the function body, which means that global variables with the same name are hidden in the entire function body.
Although local variables are defined throughout the body of the function, they are not initialized until the VAR statement is executed.
So in the above example, the function f and the following function are equivalent:

function f () {
var scope; Local variables are declared at the beginning of a function
alert (scope); The variable is defined here, but the value is still "undefined"
Scope = ' local '; Now initialize the variable and assign it a value
alert (scope); Here the variable has a value
}
This example shows why it is a good programming practice to put all the variable declarations together at the beginning of the function.

The following are from: http://www.cnblogs.com/wangfupeng1988/p/3986420.html

Code 1:

Console.log (a); Variable A is undeclared and this would occure error reporting

Code 2:

Console.log (a); Undefined, declare a variable in JavaScript and not assign a value. Then the variable ' s default value is undefined.

var A;

Code 3:

Console.log (a); Undefied

var a = 10;

Code 4:

Console.log (this); Here the output will be the Window object, the Window object is a global object

Code 5:

Console.log (F1); Function F1 () {}

Function F1 () {}; declaring functions

Code 6:

Console.log (F2); Undefined

var F2 = function F2 () {}; function expression, which produces the same effect as the declaration of the variable and assigns the value

Note: Learning C and Java all know that you must first declare the data type before using a variable  , such as: int A;

At the same time, the assignment can be initialized while declaring the variable, for example: int a = 1001;

but during the execution of the program, is the preprocessing of the type declaration, and then the execution assignment, "=" is an assignment operator.

[label][javascript][the Defined Guide of JavaScript] Variable scope

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.