Home > Web Front-end > JS Tutorial > body text

Detailed explanation of the use of closures in js

php中世界最好的语言
Release: 2018-03-23 15:50:22
Original
1497 people have browsed it

This time I will bring you a detailed explanation of the use of js closures. What are the precautions when using js closures? Here are practical cases, let’s take a look.

Closure

The Chinese meaning is to close a package. If we regard the scope of function as a package, then this word vividly reflects its role. The normal execution flow of a function is that after the statements in the function are executed, the program will automatically destroy the scope of the function. However, when another function is declared in a function, and there is a variable referencing the parent function when the sub-function is executed, A closure will be formed, which is equivalent to closing the scope of the parent function and preventing the program from destroying it.

For example:

function a() {
  var name = "xuxu";
  function b() {
    console.log(name);
  }
  // 此处产生闭包 
  b();
} 
a();
Copy after login

A closure is generated when a function can remember and access the scope chain in which it is located. Of course, most closures are not so intuitive, because the sub- The function can be called outside the parent function, for example:

function a() {
  var name = "xuxu";
  function b() {
    console.log(name);
  }
  return b;
} 
var c=a();
// 此处产生闭包 此处的c函数其实就是a函数
c();
Copy after login

Through the above code, we can also see the benefit of a closure, that is, we can access it in the global scope (here is the width) When it comes to the value of the scope of the local scope (a function), this cannot be done according to the normal lexical scope, but when we use closures, it is possible. Then let’s look at something we usually write more often:

function foo() {
  var a = 2;
  function baz() {
     // 2
    console.log( a ); 
  }
  bar( baz );
}
function bar(fn) {
// 大家快看呀,这就是闭包!
  fn(); 
}
Copy after login

or

var fn;
function foo() {
  var a = 2;
  function baz() {
    console.log( a );
  }
  // 将baz分配给全局变量
  fn = baz; 
}
function bar() {
   // 大家快看呀,这就是闭包!
  fn();
}
foo();
// 2
bar();
Copy after login

The above are also closures, so sub-functions are called inside the function, or ## is changed by some means #Internal functionIf passed outside the lexical scope, it will hold a reference to the original definition scope, and a closure will be used wherever this function is executed.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the scope and pre-parsing of js

Vue enumeration type implementation HTML

node+express implements chat room

Observer mode changes the page amount

The above is the detailed content of Detailed explanation of the use of closures in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!