当前位置:网站首页>Closure and closure function
Closure and closure function
2022-07-03 17:58:00 【Yellow sauce!】
<script>
// Closure : A function can read variables inside another function externally
function fn(){
let a = 10
// There is one A function , Again A The function returns an B function
return function(){
//B The function internally accesses A Private variables inside the function
console.log(a)
}
}
// Again A There is a variable outside the function to refer to this B function
let res = fn()
res()
// Closure effect : Extends the life cycle of variables , Protected private variables
</script>
<body>
<button> Button 1</button>
<button> Button 2</button>
<button> Button 3</button>
<script>
var aBtn = document.querySelectorAll('button')
// for(var i=0; i<aBtn.length; i++){
// // Custom properties
// aBtn[i].index = i
// aBtn[i].onclick = function(){
// console.log(this.index)
// }
// }
// Beneficial closure
// for(var i=0; i<aBtn.length; i++){
// /*
// Code execution order :
// + First execution
// => i=0, index=0, A reference data type is returned inside the function , A new function definition space is created inside the function execution space , Extended index The declaration period of variables
// The function is finished ,index Already exists in memory
// => i=1, index=1, index Already exists in memory
// => ...
// */
// function fn(index){
// return function(){
// console.log(index)
// }
// }
// aBtn[i].onclick = fn(i)
// }
// for(var i=0; i<aBtn.length; i++){
// /*
// Code execution order :
// + First execution
// => i=0, index=0, A reference data type is returned inside the function , A new function definition space is created inside the function execution space , Extended index The declaration period of variables
// The function is finished ,index Already exists in memory
// => i=1, index=1, index Already exists in memory
// => ...
// */
// // Is the combination of self executing functions and closures
// aBtn[i].onclick = (function(index){
// return function () {
// console.log(index)
// }
// })(i)
// }
// let res = (function(){
// let a = 666
// return function(){
// console.log(a)
// }
// })()
// res()
for(let i=0; i<aBtn.length; i++){
aBtn[i].onclick = function(){
console.log(i)
}
}
</script>
边栏推荐
- Kotlin's collaboration: Context
- win32:堆破壞的dump文件分析
- Automata and automatic line of non-standard design
- Tensorboard quick start (pytoch uses tensorboard)
- [set theory] order relation: summary (partial order relation | partial order set | comparable | strictly less than | covering | hasto | total order relation | quasi order relation | partial order rela
- Getting started with deops
- Leetcode 108 converts an ordered array into a binary search tree -- recursive method
- 分布式的任务分发框架-Gearman
- Kotlin的协程:上下文
- Redis core technology and practice - learning notes (VII) sentinel mechanism
猜你喜欢
Win32: dump file analysis of heap corruption
Embedded-c language-7
面试官:值为 nil 为什么不等于 nil ?
(8) HS corner detection
模块九作业
AcWing 271. 杨老师的照相排列【多维DP】
Five problems of database operation in commodity supermarket system
Leetcode 669 pruning binary search tree -- recursive method and iterative method
Research Report on competitive strategy Outlook Analysis and investment strategic planning of China's smart home equipment industry, 2022-2028
Micro service component sentinel console call
随机推荐
Gear2021 monthly update - December
Interviewer: why is the value nil not equal to nil?
Classroom attendance system based on face recognition tkinter+openpyxl+face_ recognition
OpenSSL的SSL/BIO_get_fd
PHP processing - watermark images (text, etc.)
Deops入门
Basic grammar of interview (Part 2)
WEB-UI自动化测试-最全元素定位方法
c# .net 工具生态
ES6类的继承
[vscode] convert tabs to spaces
How to deploy applications on kubernetes cluster
Supervisor monitors gearman tasks
Discussion sur la logique de conception et de mise en oeuvre du processus de paiement
[教程]在 CoreOS 上构建你的第一个应用
SDNUOJ1015
[combinatorics] recursive equation (four cases where the non-homogeneous part of a linear non-homogeneous recursive equation with constant coefficients is the general solution of the combination of po
ArrayList分析3 : 删除元素
Fedora 21 installs lamp host server
Remote office tools sharing | community essay solicitation