Scopes are one of the most important concepts of JavaScript, and to learn JavaScript you need to understand how JavaScript scopes and scopes work. Today's article is a simple introduction to JavaScript scopes and scope chains, hoping to help you learn JavaScript better.
JavaScript scopes
Any programming language has the concept of scope, simply speaking, scope is the scope of the variable and function, that is, the scope of the variable and function visibility and life cycle. In JavaScript, the scope of a variable has both global scope and local scope.
1 scope (global scope)
Objects that can be accessed anywhere in the code have global scope, typically with global scope in the following situations:
(1) A variable defined outside the outermost function and the outermost function has a global scope, for example:
var authorname= "Do not scatter guests", function dosomething () {var blogname= "alidece"; function Innersay () {alert (blogname);} Innersay ();} alert (authorname);//Do not scatter guest alert (blogname);//error dosomething ();//alideceinnersay ();//Error
(2) All variables that do not have a direct assignment defined are automatically declared to have global global scope, for example:
function dosomething () {var authorname = "Alidece"; blogname = "alidece"; alert (authorname);} DoSomething (); alert (blogname); alert (authorname);//authorname is not defined
(3) All Window object properties have global scope
In general, the built-in properties of the Window object have global scope, such as Window.name, Window.location, Window.top, and so on.
1 scope (local scope)
In contrast to the global scope, local scopes are generally accessible only within a fixed code fragment, most commonly, such as inside a function, where it is also seen that some people refer to this scope as a function scope, such as blogname and function Innersay in the following code, which have only local scopes.
function dosomething () {var blogname= "alidece"; function Innersay () {alert (blogname);} Innersay ();}
Scope chain (scope chain)
In JavaScript, functions are objects, and in fact, everything in JavaScript is an object. function objects, like other objects, have properties that can be accessed through code and a series of internal properties that are accessible only by the JavaScript engine. One of the internal properties is [[Scope]], which is defined by the ECMA-262 Standard third edition, which contains the objects and collections in the scope in which the function is created, which is called the scope chain of the function, which determines which data can be accessed.
When a function is created, its scope chain is populated with data objects that are accessible in the scope of the creation of the secondary function. For example, define a function such as the following:
function Add (num1,num2) { var sum = num1+num2; return sum; }
View Code
When the function add is created, its scope is populated with a global object that contains all the object variables, as shown (note: The picture illustrates a portion of all variables):
Not to be continued ....
Scope and scope chain in JavaScript (learn to write) [watch others ' blogs with pure hand knocking]