This is the 11th day of my participation in the August More Text Challenge. For details, see:August is more challenging


Note: Part of the content and pictures from the network, if there is any infringement, please contact me (home page has the public number: Small siege city Lion science front end)

Author: small front siege city lion, home: small front siege city lion’s home page, source: Nuggets

GitHub: P-J27, CSDN: PJ wants to be a front end siege lion

The copyright belongs to the author. Commercial reprint please contact the author to obtain authorization, non-commercial reprint please indicate the source.


The concept of Scope

  • conceptIn general terms, the scope is that of a variable or functionscope. Scope inThe function definitions, it has been determined.
  • Objective: To improve the reliability of the program and reduce naming conflicts.

Classification of scopes

In JS, there are two types of scope :(ES6 adds block-level scope)

  • Global scope: scope an entire script tag, or scope a single JS file.
  • Function scope (local scope) : The code context that operates within a function.

Access relationship of scope

Variables of the outer scope are accessible from the inner scope. Variables of the inner scope are not accessible from the outer scope. (Inside can visit outside, outside can not visit inside)

var a = 'aaa';
function foo(a) {
    var b = 'bbb';
    console.log(a); 
  // Print the result: aaa. Indicates that an inner scope can access variables in an outer scope
  // Rely on the scope chain. We'll talk about scope chains later
}
console.log(b); 
// Error: Uncaught ReferenceError: b is not defined.
// Specify that the outer scope cannot access variables in the inner scope
Copy the code

To learn more about the relationship between scopes and scope chains, check out this article and click on me


The scope of a variable

Depending on the scope, variables can be classified into two categories: global variables and local variables.

Global variables:

  • Variables declared in the global scope are called “global variables”. This variable can be accessed anywhere in the global scope.
  • inIn the global scope, variables declared with var are global.
  • In any scope, undeclared variables that are used directly will also be hung under the Window object as global variables, but this is not recommended.

Local variables:

  • Variables defined in the scope of a function are called local variables.
  • Inside functions, variables declared with var are local variables.
  • Function parameters are also local variables.

Global and local variables in terms of execution efficiency:

  • Global variables: OnlyIt will not be destroyed until the browser is closed, compare memory.
  • Local variables: when theWhen the block is finished running, it will be destroyed, save more memory space.

The hierarchy of the scope

When a variable is operated on in a function scope, it first looks for it in its own scope and uses it directly if it has one (the proximity principle). If not, look up the scope until the global scope is found. If the global scope is still not found, a ReferenceError is reported.

To access global variables in a function, use the Window object. (For example, both the global scope and the function scope define the variable a. If you want to access the global variable, use window.a.)

If you want to know the relationship between scope and scope chain, you can see this article. Click on me


Thank you for reading, I hope it can help you, if there are mistakes or infringement of the article, you can leave a message in the comment area or add the public account in my home page to contact me.

Writing is not easy, if you feel good, you can “like” + “comment” thanks for supporting ❤