JavaScript function scopes, closures, and anonymous functions 2

Source: Internet
Author: User

Add to last said: http://www.cnblogs.com/zhangmingzhao/p/7740565.html

1.

The first thing to be clear is the basic concept of JS

  1. Scope
    The main scope of JS is 全局作用域 with 函数作用域 (local scope), there is no block-level scope, but in ES6 with let declared variables have the effect of block-level scope.
  2. Execution environment, variable object
    The execution environment is divided into the global execution environment and the function execution environment, which is the execution context of the current code, which is simply defined as the permission of the variable or function to access other data, controls the visibility and life cycle of variables and functions, and each execution environment has an object that is associated with it, the variable object, All variables and functions defined in the environment are stored in this object. The global environment is the outermost, and each function has its own execution environment.
  3. Scope chain
    When code executes in an environment, it creates a scope chain for the variable object, which is always the variable object of the current execution environment, the next variable object from the containing (external) environment, and the variable object of the global execution environment is always the last object of the scope chain, like an onion, If you want to peel it off one layer at a level, it's a good idea.
  4. Execute function expression immediately
    ()This parenthesis has two meanings, one refers to an expression, and the other is the execution of the function.

    //函数表达式var fuc = function() {};//匿名函数直接执行(立即执行函数表达式)(function(){})();

The effect above is a good creation of a scope, you can not access the outside scope of the internal, as if to be blocked. So above you declare variable a in the global environment and in the function environment declaration a two there is no contact, two in different variable object inside, although the name is the same. But if you do not declare a in the function, but instead directly a = 5 assign a value, then the external a will change, because the inside of the scope chain to access the external variables and assign values. Understanding the scope chain is a good understanding of these issues.
Finally, what is a closure ?, a: Is the function that can access local variables!! The pattern is as follows (in essence or through a chain of scopes)

var funcName = function() {    return function() { }}

2.

The problem should be that the concept of JavaScript function scopes, closures, and anonymous functions is not very clear. The point is to look at the JavaScript functions section.

Let's say briefly: 1.function iterator (i) {} function name: iterator; * Note () before the iterator becomes an anonymous function, call method: iterator (parameter)

2. (Do someting ... ) () The anonymous function must be called immediately otherwise meaningless; a named function can be called by name; anonymous functions cannot always be used () to indicate a call, haha, the browser does not know what you are doing.

3. When the Dong someting in 2 is a function of a closure, two or more function nesting functions constitute a closure, the outer function of this example is the exception of the anonymous function, in addition to the function has its own scope, with functions iterator () { Scope here} For example, within curly braces for this function's scope, (anonymous scope here) (anonymous immediate invocation);

4.

var a = 6;//external scope of anonymous functions

(function () {//Internal function, also anonymous function, even a function nested to form a closure, this is the scope of the anonymous function, more specifically called the internal scope

var a = 5;

Console.log (a)//a for the 5,a method for the first layer of a layer to look out, if there is no "var a=5" at this time a=6
})();

5.function A () {

Function B () {//Closure formation

}

}

JavaScript function scopes, closures, and anonymous functions 2

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.