当前位置:网站首页>Tab bar (addeventlistener and onclick practice, used with bind method, exponential growth to listen for events)
Tab bar (addeventlistener and onclick practice, used with bind method, exponential growth to listen for events)
2022-07-27 22:21:00 【By the Difeng River】

It is :
I'm learning bind When the method is used , Try to optimize the previous Tab Column case appears bug, As shown in the figure : Click once "+" newly build Tab bar , For the first time, a new Tab bar , For the second time, two new Tab The column appears . third time 4 individual . And then there was 8,16,32 Strange operation of exponential growth .
Here are two key functions
init(){
//init The initialization operation causes the related elements to bind events
this.updateNode();
this.add.addEventListener('click',this.addTab)
for(let i=0;i<this.lis.length;i++){
this.lis[i].index=i;
this.lis[i].addEventListener('click',this.toggleTab.bind(this.lis[i],this))
this.removes[i].addEventListener('click',this.removeTab)
this.spans[i].addEventListener('dblclick',this.editTab)
this.sections[i].addEventListener('dblclick',this.editTab)
// console.log(2)
}
}
// 2. Add functionality
addTab(){
console.log(1) // Verification triggers several add events
let str=`<li><span> test ${
random}</span><span class="iconfont icon-guanbi"></span></li>`
let sect=`<section> test ${
random}</section>`
random++
that.ul.insertAdjacentHTML("beforeend",str)
that.tabscon.insertAdjacentHTML("beforeend",sect)
that.init()
// console.log( 1)
}
The number of clicks has not changed ,Tab Columns have increased exponentially
Core code problem :
Previously, by defining global variables that Pass in cosntructor Medium this, No problem . Now through bind Change the corresponding function this It points to , Omit that.
First look at the following two methods
this.add.addEventListener('click',this.addTab.bind(this.add, this))
this.add.onclick = this.addTab.bind(this.add, this);
(1).onclick Will be registered at the same time onclick Covered , Execution after triggering is an overridden event .
(2).addEventListener Will not be covered
(3).bind A new function will be generated
function fn(a, b, c) {
return a + b + c;
}
let _fn = fn.bind(null, 10);
let ans = _fn(20, 30); // 60
fn The function takes three arguments ,_fn Function will 10 As the default first parameter , So you only need to pass in two parameters , If you accidentally pass in three parameters , don 't worry , Only the first two .
function fn(a, b, c) {
return a + b + c;
}
let _fn = fn.bind(null, 10);
let ans = _fn(20, 30, 40); // 60
What's the use ? If some functions , The first few parameters have “ Make up one's mind ” 了 , We can use bind Returns a new function . in other words ,bind() It can make a function have preset initial parameters . These parameters ( If any ) As bind() The second parameter of follows this Back , Then they will be inserted at the beginning of the parameter list of the objective function , The parameters passed to the binding function will follow them
function list() {
return Array.prototype.slice.call(arguments);
}
let list1 = list(1, 2, 3); // [1, 2, 3]
// Create a function with a preset leading argument
let leadingThirtysevenList = list.bind(undefined, 37);
let list2 = leadingThirtysevenList(); // [37]
let list3 = leadingThirtysevenList(1, 2, 3); // [37, 1, 2, 3]
Analyze and solve :
If only because addEventListener Will not cover , So there will be a right “+” There are multiple listening events . Then why use global variables that collocation addEventListener There are no mistakes ?
Change the collocation method below , Add two listening functions , Test click once “+”,Tab Column addition
Collocation :addEventListener and that ( Results added normally 1 Columns , Description repeated listening is invalid )
init(){
//init The initialization operation causes the related elements to bind events
this.updateNode();
this.add.addEventListener('click',this.addTab)
this.add.addEventListener('click',this.addTab) // Add two listening functions , It has no effect on
for(let i=0;i<this.lis.length;i++){
this.lis[i].index=i;
this.lis[i].addEventListener('click',this.toggleTab.bind(this.lis[i],this))
this.removes[i].addEventListener('click',this.removeTab)
this.spans[i].addEventListener('dblclick',this.editTab)
this.sections[i].addEventListener('dblclick',this.editTab)
// console.log(2)
}
}
Collocation :addEventListener and bind(2,6,18,54 With 3 The exponential growth of ) I fuck , People are stupid
init(){
//init The initialization operation causes the related elements to bind events
this.updateNode();
// Add two listening functions , It has no effect on
this.add.addEventListener('click',this.addTab.bind(this.add, this))
this.add.addEventListener('click',this.addTab.bind(this.add, this))
for(let i=0;i<this.lis.length;i++){
this.lis[i].index=i;
this.lis[i].addEventListener('click',this.toggleTab.bind(this.lis[i],this))
this.removes[i].addEventListener('click',this.removeTab)
this.spans[i].addEventListener('dblclick',this.editTab)
this.sections[i].addEventListener('dblclick',this.editTab)
// console.log(2)
}
}
Collocation :onclick and bind
this.add.onclick = this.addTab.bind(this.add, this);
this.add.onclick = this.addTab.bind(this.add, this);
Collocation :onclick and that
this.add.onclick = this.addTab;
this.add.onclick = this.addTab;
Add a column normally for both
The current solution is to change to the following code
this.add.onclick = this.addTab.bind(this.add, this);
I think it's because there are two kinds of monitoring operations , about bind for ,bind Each execution results in a new function , What changes is the new function this Point to .onclick Is a function assignment , Even if the function changes , Always assign to onclick, and addEventListener Not assignment , Every time bind After that, different listening event functions are generated .
边栏推荐
- SQL注入 Less29(参数污染绕过WAF)
- Learn the use principle and core idea of thread pool from the source code
- Chapter 3 business function development (choose to export market activities, Apache POI)
- Station B collapsed. What did the developer responsible for the repair do that night?
- JVM memory model interview summary
- EC code introduction
- 阿里资深软件测试工程师推荐测试人员必学——安全测试入门介绍
- 视频直播源码,uni-app实现广告滚动条
- Leetcode-152- product maximum subarray
- Window localStorage 属性和Location 对象
猜你喜欢

8000字讲透OBSA原理与应用实践

Inventory Poka ecological potential project | cross chain characteristics to promote the prosperity of multi track

Deepfake 换脸真假难辨,马斯克分克已伪装成功

JVM memory model interview summary

How to use Fiddler for weak network testing
![[question 23] Sudoku game with rotation | DFS (Beijing Institute of Technology / Beijing Institute of Technology / programming methods and practice / primary school)](/img/75/c207f4f562fd5b547c5b3134113154.jpg)
[question 23] Sudoku game with rotation | DFS (Beijing Institute of Technology / Beijing Institute of Technology / programming methods and practice / primary school)

ApacheSpark-命令执行(CVE-2022-33891) 漏洞复现

8000 word explanation of OBSA principle and application practice

Alibaba Senior Software Testing Engineer recommends testers to learn -- Introduction to security testing

Temperature relay
随机推荐
STM32 project Sharing -- mqtt intelligent access control system (including app control)
How to learn object Defineproperty | an article takes you to quickly learn
项目管理工具禅道
Leetcode-155-minimum stack
【StoneDB故障诊断】系统资源瓶颈诊断
舌簧继电器
Finish learning redis cluster solution at one go
Log4j vulnerability is still widespread and continues to cause impact
Cocos: simple application of ccpdistance function and the function of eyeball rotating in orbit with fingers
An2021 software installation and basic operation (new file / export)
九天后我们一起,聚焦音视频、探秘技术新发展
【StoneDB故障诊断】MDL锁等待
How to buy stocks on mobile phones? Is it safe to open an account
Pythia: Facebook's latest open source visual and language multitasking learning framework
The design idea of relational database is obvious to you in 20 pictures
Lvs+kept highly available cluster
Solid state relay
How to customize logging of.Net6.0
固体继电器
Leetcode383 ransom letter