Today we will talk about event delegation and deep and shallow copy in JavaScript 1. Event delegationFirst, let's introduce event binding //Method 1: via onclick <button onclick="clickEvent()">Click</button> <script> function clickEvent(){ alert("click event"); } </script> //Method 2: via addEventListener <button id="btn_button">Click</button> <script> var btn = document.getElementById("btn_button"); btn.addEventListener("click", function () { alert("click"); }, false); </script> Tell me the difference [function method1() { console.log("method1"); } function method2() { console.log("method2"); } function method3() { console.log("method3"); } var btn = document.getElementById("btn_button"); btn.addEventListener("click", method1, false); //The third parameter is the default value, which is bubbling by default. If it is set to true, it is captured. btn.addEventListener("click", method2, false); btn.addEventListener("click", method3, false); //Finally it will be executed in order: method1 -> method2 -> method3 btn.removeEventListener("click", method1, false); // used to remove events btn.onclick = method1; btn.onclick = method2; btn.onclick = method3; //Finally only method3 will be executed btn.onclick = null; // used to remove the event Event Bubbling<ul id="container" style="display: inline-block;"> <li id="title1">123456</li> <li id="title2">qwert</li> <li id="title3"> <ul> <li id="title3inner">Text inside title3</li> </ul> </li> </ul> <script> var container = document.getElementById("container"); var title1 = document.getElementById("title1"); var title2 = document.getElementById("title2"); var title3 = document.getElementById("title3"); var title3inner = document.getElementById("title3inner"); container.onclick = function (e) { alert("container"); } title1.onclick = function (e) { alert("title1"); } title2.onclick = function (e) { alert("title2"); } title3.onclick = function (e) { alert("title3"); } title3inner.onclick = function (e) { alert("title3inner"); } </script> Clicking on the text in title3 will trigger 3 events, popping up "title3inner" -> "title3" -> "container" respectively. Event CaptureAs for event bubbling, the reverse is event capture, that is, clicking on the "text inside title3" will trigger 3 events, popping up "container" -> "title3" -> "title3inner" respectively. Sublimation of event bubblingUse of event delegation <ul id="container" style="display: inline-block;"> <li id="title1">123456</li> <li id="title2">qwert</li> <li id="title3">QWE123</li> </ul> <script> var container = document.getElementById("container"); container.onclick = function (e) { //console.log(e); if (e.target.id = "title1") { alert(e.target.innerText); } else if (e.target.id = "title2") { alert(e.target.innerText); } else if (e.target.id = "title3") { alert(e.target.innerText); } } </script> As for the advantage, it is that you only need to register a click event, and then use the target to determine the specific element clicked. The currentTarget refers to the element that registered the event. think In theory, using event delegation is indeed an optimization. You only need to register an event and then implement the corresponding function through event bubbling. Anyway, I did it this way, binding an event to each button and then preventing bubbling. In practice, I personally don’t consider event delegation very much. I don’t think it is necessary. After all, there are not so many events. 2. Deep and shallow copyAs for the knowledge of deep and shallow copying, I will make it short and kill with one blow! let a = 3; let b = a; a = 4; console.log(a); //4 console.log(b); //3 console.log(a === b); //This is a deep copy, a and b are completely different variables, each storing a value. let arr = [1, 2, 3, 4, 5]; let brr = arr; arr[1] = 8; console.log(arr[1]); //8 console.log(brr[1]); //8 Why? This is a shallow copy. The array type is a reference type. The arr and brr variables only store reference addresses. They point to the array [1,2,3,4,5] together. console.log(arr === brr); //true In practice, if you want to implement a deep copy, how do you do it? let arr = [1, 2, 3, 4, 5]; let brr = JSON.parse(JSON.stringify(arr)); arr[1] = 8; console.log(brr[1]); //2, this implements the so-called deep copy ConclusionAs the interviewer said, although the above knowledge may not be used in actual work, you still need to know it! Still need to know! ! Still need to know! ! ! This concludes this article on the event delegation mechanism and deep and shallow copying in JavaScript. For more relevant js event delegation and deep and shallow copying content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Install mysql5.7.13 using RPM in CentOS 7
>>: Pitfalls encountered when installing MySQL 5.7.17 compressed version under Windows
When learning Vue, when I always use webpack inst...
Three ways to use CSS in HTML: 1. Inline style: s...
Table of contents Native JS How to send a get req...
Table of contents 【Effect】 【Implementation method...
Error screenshot Can't find where the excepti...
Table of contents Problem Description Rendering T...
The worst option is to sort the results by time a...
background First of all, I would like to state th...
This article shares the installation tutorial of ...
Copy code The code is as follows: <iframe id=&...
Related Articles: Website Design for User Experien...
The default submission method of html is get inste...
This article example shares the specific code of ...
1. Create a scheduling task instruction crontab -...
1. Import mysql command The mysql command import ...