scope
Simply put, the scope is the legal scope of use of variables. Scope is divided into three types: global scope, function scope and block-level scope (es6 new)
- Global scope: Variables declared globally (outside the function) have a global scope, which is not bound by the scope of the function and can be used anywhere.
- Function scope: A variable declared inside a function has function scope, and it can only be used inside the current function.
- Block-level scope (new in ES6): Variables declared in code blocks have block-level scope, which can only be used in the current code block (for example: if, for, while and switch, etc. are wrapped in {} curly braces code block)
scope chain
When using a variable, it will first search in the current scope, and return the value if the variable is found, otherwise it will search in the parent scope, and search up one layer at a time until it is found, if it is finally in the global role If the variable cannot be found under the domain, the error xxx is not defined will be reported. It is this way of searching up layers to form a chain structure. We call this a scope chain.