Home  >  Article  >  Web Front-end  >  Simple understanding of JS closures

Simple understanding of JS closures

小云云
小云云Original
2018-02-24 14:28:152364browse

Closure is a very important concept in JS. My personal understanding is the environment for access control of variables between function callers. This article will briefly introduce what JS closure is.

function Person(){
  var name='stt';
  function sayName(){
    console.log('name is=',name);
  };
  sayName();
}
var person=new Person();
person();

The name in the sayName function is a local variable defined by the external function, and sayName can directly access the variable

Advantages: 1. The local variables inside the Person function can be accessed through the person() call name

2. The declared local variable name will not be recycled with the end of the Person function because it is referenced by sayName, and will always exist in the memory

Disadvantages: Frequent use of closures , will cause many variables to reside in memory, affecting performance

Related recommendations:

A simple understanding of js closure

JS closure Detailed explanation of common forms of packages

Sample code sharing of JS closure usage

The above is the detailed content of Simple understanding of JS closures. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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