当前位置:网站首页>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
边栏推荐
猜你喜欢
学习笔记|数据小白使用DataEase制作数据大屏
关于测试人生的一站式发展建议
Socket socket programming
Static semantic check of clang tidy in cicd
seata 1.3.0 四種模式解决分布式事務(AT、TCC、SAGA、XA)
数据库同步工具 DBSync 新增对MongoDB、ES的支持
The opacity value becomes 1%
Antd select selector drop-down box follows the scroll bar to scroll through the solution
使用MeterSphere让你的测试工作持续高效
Wallhaven壁纸桌面版
随机推荐
自动化测试框架
Android 面试知识点
About the application of writing shell script JSON in JMeter
RationalDMIS2022 高级编程宏程序
Use metersphere to keep your testing work efficient
Case study of Jinshan API translation function based on retrofit framework
Transaction rolled back because it has been marked as rollback-only解决
JSON format query of MySQL
vim 的各种用法,很实用哦,都是本人是在工作中学习和总结的
Idea shortcut keys
[untitled]
Multithreaded application (thread pool, singleton mode)
uniCloud
高考作文,高频提及科技那些事儿……
Graduation season | keep company with youth and look forward to the future together!
Go Slice 比较
uniCloud
Verilog realizes nixie tube display driver [with source code]
The post-90s resigned and started a business, saying they would kill cloud database
【问道】编译原理