当前位置:网站首页>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>
边栏推荐
- The second largest gay dating website in the world was exposed, and the status of programmers in 2022
- PR second time
- TCP congestion control details | 3 design space
- MinGW compile boost library
- STM32 realizes 74HC595 control
- OpenSSL的SSL/BIO_get_fd
- Ml (machine learning) softmax function to realize the classification of simple movie categories
- 1146_ SiCp learning notes_ exponentiation
- SSL / bio pour OpenSSL Get FD
- Leetcode 669 pruning binary search tree -- recursive method and iterative method
猜你喜欢
The second largest gay dating website in the world was exposed, and the status of programmers in 2022
Redis core technology and practice - learning notes (VII) sentinel mechanism
BFS - topology sort
[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
1146_ SiCp learning notes_ exponentiation
Leetcode 669 pruning binary search tree -- recursive method and iterative method
How to deploy applications on kubernetes cluster
PHP MySQL preprocessing statement
How to install PHP on Ubuntu 20.04
基于人脸识别的课堂考勤系统 tkinter+openpyxl+face_recognition
随机推荐
How to deploy applications on kubernetes cluster
圖像24比特深度轉8比特深度
Web-ui automated testing - the most complete element positioning method
[linux]centos 7 reports an error when installing MySQL "no package MySQL server available" no package ZABBIX server MySQL available
i++与++i的区别:通俗易懂的讲述他们的区别
Research Report on investment trends and development planning of China's thermal insulation material industry, 2022-2028
STM32 realizes 74HC595 control
Global and Chinese pediatric palliative care drug market development research and investment planning recommendations report 2022-2028
互聯網醫院HIS管理平臺源碼,在線問診,預約掛號 智慧醫院小程序源碼
[vscode] convert tabs to spaces
Embedded-c language-7
Applet with multiple tabs and Swipers + paging of each tab
一入“远程”终不悔,几人欢喜几人愁。| 社区征文
PHP MySQL preprocessing statement
聊聊支付流程的设计与实现逻辑
Ssl/bio of OpenSSL_ get_ fd
聊聊支付流程的设计与实现逻辑
Implementation of Tetris in C language
PHP processing - watermark images (text, etc.)
WebView module manages the application window interface to realize the logical control and management operation of multiple windows (Part 1)