当前位置:网站首页>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>
边栏推荐
- Automata and automatic line of non-standard design
- 统计图像中各像素值的数量
- [LINUX]CentOS 7 安装MYSQL时报错“No package mysql-server available“No package zabbix-server-mysql availabl
- PHP MySQL preprocessing statement
- Module 9 operation
- Fedora 21 installs lamp host server
- List的stream中Long对象与long判等问题记录
- SSL / bio pour OpenSSL Get FD
- Talk about the design and implementation logic of payment process
- [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
猜你喜欢
Internet Hospital his Management Platform source, online Inquiry, appointment Registration Smart Hospital Small program source
Lesson 13 of the Blue Bridge Cup -- tree array and line segment tree [exercise]
Embedded-c language-7
Redis core technology and practice - learning notes (11): why not just string
Draw some simple graphics with MFC
Research Report on market demand and investment planning for the development of China's office chair industry, 2022-2028
Implementation of Tetris in C language
Five problems of database operation in commodity supermarket system
Research on Swift
How to deploy applications on kubernetes cluster
随机推荐
Research Report on competitive strategy Outlook Analysis and investment strategic planning of China's smart home equipment industry, 2022-2028
[combinatorics] generating function (property summary | important generating function)*
SDNUOJ1015
OpenSSL的SSL/BIO_get_fd
[combinatorics] generating function (summation property)
PHP processing - watermark images (text, etc.)
Managing multiple selections with MVVM - managing multiple selections with MVVM
Leetcode Valentine's Day Special - looking for a single dog
Graduation summary
Automata and automatic line of non-standard design
The difference between i++ and ++i: tell their differences easily
Fedora 21 installs lamp host server
(9) Opencv Canny edge detection
[vscode] convert tabs to spaces
Image 24 bit depth to 8 bit depth
Keepalived 设置不抢占资源
Discussion sur la logique de conception et de mise en oeuvre du processus de paiement
PHP MySQL reads data
ArrayList分析3 : 删除元素
link preload prefetch