当前位置:网站首页>What is a closure function
What is a closure function
2022-06-23 09:14:00 【Crazy_ GirlLL】
What is a closure function
1、 Definition and Usage : When the return value of one function is another function , If the returned function calls other variables inside its parent function , If the returned function is executed externally , There's a closure .( The concept blogger excerpts the above points , View details Baidu Encyclopedia )
2、 form : Enable the external function to call the variable defined inside the function .
Case study 1: According to the rules of the scope chain , The underlying scope has no declared variables , I'll go up to the next level , Find and return , If you don't find it, you keep looking for , until window The variable of , Return on no undefined. There are obvious variables here a It's internal to the function fun The variable of the a .
( Scope classification of variables : Global and local variables .)
* notes :* When variables are declared inside a function , Be sure to use var command . If not , You actually declare a global variable !

Case study 2: Different from case one fun2 In closure object , Function called at this time nbfun2 It's just an internal function method , So we define a variable name After the closure function object is called , take nbfun2 The function is assigned to name Variable ; And then use name() Method .

Case study 3:
First step : When we call a closure fun3 Object time , An introduction 1 An introduction 2 Give the returned function objects to the variables respectively a b;
The second step : At this point, the internal function call is output from the console Pass in separately 1 2 Value to a1 Of y b2 Of y;
( At this time, that is to say a1 b2 All are closures. They share the same function definitions , But it is stored in a different environment )
stay a1 in x=1, b2 in x=2;

summary :
Closures are mainly used to design private methods and variables . The advantage of closures is that they can avoid the pollution of global variables , The disadvantage is that closures are memory resident , Increases memory usage , Improper use can easily cause memory leakage . stay js in , Function is closure , Only functions produce the concept of scope
Closures have three characteristics :
Function nested function
Functions can reference external parameters and variables internally
Parameters and variables are not recycled by the garbage collection mechanism
边栏推荐
- A method of realizing video call and interactive live broadcast in small programs
- GeoServer adding mongodb data source
- Redis learning notes - Database Management
- 如何在 FlowUs、Notion 等笔记软件中使用矩阵分析法建立你的思维脚手架
- Lua的基本使用
- Redis learning notes - redis and Lua
- 玩转NanoPi 2 裸机教程编程-01点亮User LED难点解析
- Use of type dependent names must be prefixed with 'typename'
- Redis学习笔记—数据库管理
- 一个采用直接映射方式的32KB缓存......存储器课后习题
猜你喜欢
随机推荐
June 22, 2022: golang multiple choice question, what does the following golang code output? A:3; B:1; C:4; D: Compilation failed.
ionic5表单输入框和单选按钮
Redis learning notes - slow query analysis
【云原生 | Kubernetes篇】Kubernetes原理与安装(二)
Custom tags - JSP tag enhancements
2022-06-22:golang选择题,以下golang代码输出什么?A:3;B:1;C:4;D:编译失败。
一个采用直接映射方式的32KB缓存......存储器课后习题
'教练,我想打篮球!' —— 给做系统的同学们准备的 AI 学习系列小册
RGB与CMYK颜色模式
学习SCI论文绘制技巧(F)
Quartz Crystal Drive Level Calculation
How to use matrix analysis to build your thinking scaffold in flowus, notation and other note taking software
JSP入门总结
【NanoPi2试用体验】裸机第一步
学习SCI论文绘制技巧(E)
Map接口的注意事项
如何在 FlowUs、Notion 等笔记软件中使用矩阵分析法建立你的思维脚手架
A method of realizing video call and interactive live broadcast in small programs
Node request module cookie usage
Detailed explanation of srl16e in xilinxffpga






