当前位置:网站首页>Briefly introduce closures and some application scenarios
Briefly introduce closures and some application scenarios
2022-07-07 11:17:00 【InfoQ】
Closure
Preface
What is a closure ?
function drink() {
var beerName = " Snow Beer ";
let beerNum1 = 1;
const beerNum2 = 2;
var innerBeer = {
getBeer:function() {
console.log(beerNum1)
return beerName
},
setBeer:function(newBeer) {
beerName = newBeer
}
}
return innerBeer
}
var go = drink();
go.setbeer(" Stuffy donkey ");
go.getBeer();
console.log(go.getBeer())
Why closures are prone to memory leaks ?
var go = drink();
Why use closures ?
Application of closures
- Mimic block level scope
- An application of timer
for (var i = 0; 1 < 10; i++) {
(function (j) {
setTimeout(function () {
console.log(j);
}, 1000 * j)
})(i)
}
- Print a set of li The subscript
for(var i = 0; i < lis.length; i++) {
(function(j) {
lis[j].onclick = function() {
alert(j)
}
})(i)
}
- Buried point counter
- The product is a common data collection method for website analysis
function count() {
var num = 0;
return function() {
return ++num
}
}
var getNum = count(); // The first place that needs statistics
var getNewNum = count(); // The second place that needs statistics
// If we count two button Of hits
document.querySelectorAll('button')[0].onclick = function() {
console.log(' Click button 1 The number of times :'+getNum());
}
document.querySelectorAll('button')[0].onclick = function() {
console.log(' Click button 2 The number of times :'+getNewNum());
}
- currying
- The method of transforming a multi parameter function into a single parameter function , Make it more flexible and convenient
// Primitive function Used to check whether the text conforms to the specification
// reg The regular expression passed in txt Text to be detected
function check(reg,txt){
return reg.test(txt)
}
console.log(check( Regularity of phone numbers ,13923456789));
console.log(check( Regular mailbox ,[email protected]));
// Today,
function nowCheck(reg){
return function(txt){
return reg.test(txt)
}
}
var isPhone = nowCheck( Regularity of phone numbers )
console.log(isPhone('13923456789'))
var isEmail = nowCheck( Regular mailbox )
console.log(isEmail('[email protected]'))
summary : To put it simply, closures
边栏推荐
猜你喜欢
90后,辞职创业,说要卷死云数据库
Basic knowledge of process (orphan, zombie process)
Socket socket programming
[untitled]
2021-04-23
对比学习之 Unsupervised Learning of Visual Features by Contrasting Cluster Assignments
[STM32] actual combat 3.1 - drive 42 stepper motors with STM32 and tb6600 drivers (I)
通过 Play Integrity API 的 nonce 字段提高应用安全性
[untitled]
JSON format query of MySQL
随机推荐
Graduation season | keep company with youth and look forward to the future together!
Vscode 尝试在目标目录创建文件时发生一个错误:拒绝访问【已解决】
Verilog realizes nixie tube display driver [with source code]
The eighth training assignment
Avoid mutating a prop directly since the value will be overwritten whenever the parent component
electron添加SQLite数据库
什么是高内聚、低耦合?
uniCloud
普通测试年薪15w,测试开发年薪30w+,二者差距在哪?
关于jmeter中编写shell脚本json的应用
Interprocess communication (IPC)
When initializing 'float', what is the difference between converting to 'float' and adding 'f' as a suffix?
TDengine 社区问题双周精选 | 第二期
The post-90s resigned and started a business, saying they would kill cloud database
QT implements the delete method of the container
基于DE2 115开发板驱动HC_SR04超声波测距模块【附源码】
分布式数据库主从配置(MySQL)
Force buckle 1002 Find common characters
uniapp 在onLaunch中跳转页面后,点击事件失效解决方法
Deconstruction and assignment of variables