当前位置:网站首页>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 - data type: ordered set (Zset)
- 也无风雨也无晴
- MQTT+Flink实现实时消息的订阅与发布
- 438. Find All Anagrams in a String
- 36 krypton launched | cloud native database company "tuoshupai" completed a new round of strategic financing, and the valuation has reached the level of quasi Unicorn
- General paging (1)
- How postman does interface testing 1: how to import swagger interface documents
- 自定义标签——jsp标签增强
- 2022.6.22-----leetcode. five hundred and thirteen
- Mqtt+flink to subscribe and publish real-time messages
猜你喜欢

披萨订购设计----简单工厂模式

cooding代码库的使用笔记

【活动报名】SOFAStack × CSDN 联合举办开源系列 Meetup ,6 月 24 日火热开启

Redis learning notes - client communication protocol resp
Redis学习笔记—Redis与Lua
Redis learning notes - publish and subscribe

js 用**遮罩身份证以及手机号的重要数据

学习SCI论文绘制技巧(E)

Flink error --caused by: org apache. calcite. sql. parser. SqlParseException: Encountered “time“
Redis learning notes pipeline
随机推荐
Flink error --caused by: org apache. calcite. sql. parser. SqlParseException: Encountered “time“
[qnx hypervisor 2.2 user manual]6.1 using the QNX hypervisor system
Redis学习笔记—客户端通讯协议RESP
Vue3表单页面利用keep-alive缓存数据的一种思路
微信小程序:点击按钮频繁切换,重叠自定义markers,但是值不改变
自定义标签——jsp标签增强
如何在 FlowUs、Notion 等笔记软件中使用「番茄工作法」?
类型从属名称的使用必须以“typename”为前缀
Redis学习笔记—数据类型:集合(set)
Redis learning notes - client communication protocol resp
MQTT+Flink实现实时消息的订阅与发布
3、 System analysis and design
MySQL故障案例 | mysqldump: Couldn’t execute ‘SELECT COLUMN_NAME
Cookie和Session入门
三层架构与SSM之间的对应关系
ARM处理器与51单片机程序编写的区别
js 用**遮罩身份证以及手机号的重要数据
node request模块cookie使用
'教练,我想打篮球!' —— 给做系统的同学们准备的 AI 学习系列小册
Redis学习笔记—数据库管理