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!