当前位置:网站首页>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>
边栏推荐
- [combinatorics] recursive equation (the non-homogeneous part is an exponential function and the bottom is the characteristic root | example of finding a special solution)
- ES6类的继承
- 统计图像中各像素值的数量
- [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
- SSL / bio pour OpenSSL Get FD
- MySQL has been stopped in the configuration interface during installation
- Five problems of database operation in commodity supermarket system
- Leetcode540: a single element in an ordered array
- [combinatorics] recursive equation (summary of the solution process of recursive equation | homogeneous | double root | non-homogeneous | characteristic root is 1 | exponential form | the bottom is th
- How to enforce parameters in PowerShell- How do I make parameters mandatory in PowerShell?
猜你喜欢

聊聊支付流程的设计与实现逻辑

面试官:值为 nil 为什么不等于 nil ?

Classroom attendance system based on face recognition tkinter+openpyxl+face_ recognition

Embedded-c language-7

How to deploy applications on kubernetes cluster

Research Report on investment trends and development planning of China's thermal insulation material industry, 2022-2028

STM32 realizes 74HC595 control

PHP MySQL inserts multiple pieces of data

Deops入门

How to purchase Google colab members in China
随机推荐
Graduation summary
Analyse ArrayList 3: suppression d'éléments
MinGW compile boost library
The second largest gay dating website in the world was exposed, and the status of programmers in 2022
小程序 多tab 多swiper + 每个tab分页
Distributed task distribution framework gearman
Mathematical formula (test)
As soon as we enter "remote", we will never regret, and several people will be happy and several people will be sad| Community essay solicitation
The third day of writing C language by Yabo people
List的stream中Long对象与long判等问题记录
ArrayList analysis 3: delete elements
Automata and automatic line of non-standard design
Ssl/bio of OpenSSL_ get_ fd
聊聊支付流程的設計與實現邏輯
一入“远程”终不悔,几人欢喜几人愁。| 社区征文
模块九作业
Kotlin的协程:上下文
Tensorboard quick start (pytoch uses tensorboard)
Line by line explanation of yolox source code of anchor free series network (6) -- mixup data enhancement
How to enforce parameters in PowerShell- How do I make parameters mandatory in PowerShell?