当前位置:网站首页>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
边栏推荐
- Redis learning notes - geographic information location (GEO)
- Redis learning notes - redis and Lua
- 【活动报名】SOFAStack × CSDN 联合举办开源系列 Meetup ,6 月 24 日火热开启
- Intelligent operation and maintenance exploration | anomaly detection method in cloud system
- Leetcode topic analysis contains duplicate III
- 173. Binary Search Tree Iterator
- 三层架构与SSM之间的对应关系
- 1、 Software architecture evaluation
- S5P4418裸机编程的实现(替换2ndboot)
- Redis学习笔记—Redis与Lua
猜你喜欢

Servlet-02 生命周期

JSP入门总结

一个采用直接映射方式的32KB缓存......存储器课后习题

297. Serialize and Deserialize Binary Tree
Redis learning notes - single key management

'coach, I want to play basketball!'—— AI Learning Series booklet for system students

"Coach, I want to play basketball" -- AI Learning Series booklet for students who are making systems

Map接口的注意事项

Redis学习笔记—客户端通讯协议RESP
Redis learning notes - publish and subscribe
随机推荐
MySQL故障案例 | mysqldump: Couldn’t execute ‘SELECT COLUMN_NAME
Flink错误--Caused by: org.apache.calcite.sql.parser.SqlParseException: Encountered “time“
Redis learning notes - data type: hash
528. Random Pick with Weight
2022-06-22:golang选择题,以下golang代码输出什么?A:3;B:1;C:4;D:编译失败。
力扣之滑动窗口《循序渐进》(209.长度最小的子数组、904. 水果成篮)
1、 Software architecture evaluation
Community article | mosn building subset optimization ideas sharing
12个球,有一个与其他不一样,提供一个天平,三次找出来
Leetcode topic analysis 3sum
36氪首发|云原生数据库公司「拓数派」完成新一轮战略融资,估值已达准独角兽级别
How to use "tomato working method" in flowus, notation and other note taking software?
node request模塊cookie使用
Redis learning notes - single key management
Best time to buy and sell stock
General paging (1)
Servlet-02 生命周期
Redis学习笔记—数据类型:集合(set)
Unity grid programming 08
通用分页(1)