JavaScript scope, scope chain understanding

Source: Internet
Author: User
Tags define object define function functions log variables variable variable scope


JavaScript scopes are the accessible scope of variables and functions.



1. Variable Scope



In JavaScript, variable scopes are divided into global scope and local scope.



Global scope



You can define a variable with global scope Anywhere



1. Variables not declared with VAR (except for function parameters) have global scope and become global variables, so declaring local variables must be in var



All properties of 2.window have global scope



3. Variables declared outside the outermost function also have global scope


var globalscope= "Globalscope";
function Checkscope () {
var partscope= "part";
Scope= "Scope";
}
Checkscope ();
Console.log (Globalscope);//globalscope, global variable
console.log (scope); Scope, global variable
console.log (partscope);//uncaught Referenceerror:partscope is not defined, error: Not defined Partscope


Local scope



1. Variables declared in the function body have local scope and become local variables



2. The parameters of the function also have a local scope


var globalscope= "Globalscope";

function Checkscope (x) {
scope= "scope";//becomes global variable
var partscope= "part";//In function body, declare local variable must add VA        R
var globalscope= "Partscope";
}
Checkscope (a);
Console.log (Globalscope);//globalscope, in the body of a function, the precedence of local variables is higher than the global variable
console.log (scope),//scope, global variable Console.log (        Partscope);
Console.log (Partscope);//uncaught referenceerror:partscope is not defined, outside of local variable access
console.log (x);//Outside local variable The face is not accessible


Summary: Local variables in the function body have precedence over global variables of the same name, declaring that local variables must be in Var



2. Function scopes



Some Class C languages are block-level scopes , and each curly brace is a scope, and the code inside the curly braces is invisible to the outside world. and



JavaScript is a functional scope (function scope) and does not have a block-level scope. Regardless of where the variable in the function is declared, it is visible to the entire function, that is, all variables declared in the JavaScript function are advanced to the top of the function body, only the advance variable declaration, and the assignment of the variable remains in the original position .



A function scope can only declare a stand-alone scope with a function, not every curly brace is a separate scope, for example: The For loop does not create a local scope


for (Var i=0;i<5;i++) {
var scope= ' scope ';
Console.log (i)//5;i still exists because JavaScript is a function scope rather than a block-level scope
console.log (scope);//scope;scope still has function

Checkscope () {
console.log (funscope);//undefined, all variables declared in the function are advanced to the top of the function body, so funscope will assume that it already exists
var funscope= "Funscope";
}
Checkscope ();


3. Scope Chain



scope chain : JavaScript variables are properties of objects, which may be properties of other objects, and all objects are properties of global Objects , so the relationship of these objects can be considered as a chain,
The chain is the object where the variable is, and the end of the chain is the global object


function Checkscope (scope) {               
    var funscope= "Funscope";
    var s=scope;
}


When the value of the variable funscope is found, the current object is found, and if the current object is not found, it continues to look for the next object on the scope chain, and if no object on the scope chain contains this property, a reference error is thrown.



There are two objects on the scope chain in the body that does not contain nested functions:



1. Objects that define function parameters and local variables



2. Global objects



In a nested function body, there are at least three objects on the scope chain




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.