当前位置:网站首页>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
边栏推荐
猜你喜欢

【pyqt】tableWidget里的cellWidget使用信号与槽机制

基于华为云IOT设计智能称重系统(STM32)

【C#】WinForm运行缩放(变糊)的解决方法

JS add spaces to the string

Idea shortcut keys

Shardingsphere sub database and table examples (logical table, real table, binding table, broadcast table, single table)

How to remove addition and subtraction from inputnumber input box
![[untitled]](/img/f9/18b85ad17d4c560f2b9d95a53ee72a.jpg)
[untitled]

使用MeterSphere让你的测试工作持续高效

普通测试年薪15w,测试开发年薪30w+,二者差距在哪?
随机推荐
如何在博客中添加Aplayer音乐播放器
Add a self incrementing sequence number to the antd table component
Mysql的json格式查询
[untitled]
Introduction to shell programming
2021-05-21
解决VSCode只能开两个标签页的问题
[C #] the solution of WinForm operation zoom (blur)
Learning notes | data Xiaobai uses dataease to make a large data screen
面试被问到了解哪些开发模型?看这一篇就够了
[untitled]
互联网协议
Android interview knowledge points
变量的解构赋值
Wallhaven壁纸桌面版
Antd select selector drop-down box follows the scroll bar to scroll through the solution
uniCloud
Deconstruction and assignment of variables
请问申购新股哪个证券公司开户是最好最安全的
[untitled]