A brief analysis of the event delegation mechanism and deep and shallow copying in JavaScript

A brief analysis of the event delegation mechanism and deep and shallow copying in JavaScript

Today we will talk about event delegation and deep and shallow copy in JavaScript

1. Event delegation

First, 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
onclick can only bind one event, while addEventListener can bind multiple events at the same time

[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 Capture

As 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 bubbling

Use 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.
target is the element you actually clicked
currentTarget is the element bound to the event. If event delegation is not used, in a list, add a button after each data, and then register an event for each button. If each element is bound to an event, so many buttons will definitely affect the front-end performance. At this time, the delegate is definitely the best choice, after all, the delegate only needs to register one 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.
As for the Vue project, should event delegation be used? There seems to be a controversy here.
Some people say that it has been done for you in Vue, while others say it hasn’t. I haven’t read the source code, so I don’t know.

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 copy

As 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?
The main method is to assign values ​​through recursion, and the other is to implement it through the two methods JSON.stringify and JSON.parse.
Using the second method here is the simplest, after all, simple and violent, and often the most effective solution.

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

Conclusion

As 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:
  • Detailed explanation of deep and shallow copy of Javascript and jQuery
  • Detailed analysis of deep and shallow copies in JavaScript
  • Detailed explanation of the deep and shallow copy process in javascript
  • Brief analysis of deep and shallow copy problems in js
  • js implements deep and shallow copy of arrays and objects
  • Detailed explanation of the most complete deep and shallow copy implementation in JavaScript

<<:  Install mysql5.7.13 using RPM in CentOS 7

>>:  Pitfalls encountered when installing MySQL 5.7.17 compressed version under Windows

Recommend

How to use CSS styles and selectors

Three ways to use CSS in HTML: 1. Inline style: s...

Detailed examples of ajax usage in js and jQuery

Table of contents Native JS How to send a get req...

Example of Vue implementing fixed bottom component

Table of contents 【Effect】 【Implementation method...

Detailed explanation of three ways to wrap text in el-table header

Table of contents Problem Description Rendering T...

Mysql query the most recent record of the sql statement (optimization)

The worst option is to sort the results by time a...

Example of implementing todo application with Vue

background First of all, I would like to state th...

MySQL 8.0.12 Simple Installation Tutorial

This article shares the installation tutorial of ...

How to point the target link of a tag to iframe

Copy code The code is as follows: <iframe id=&...

How to ensure the overall user experience

Related Articles: Website Design for User Experien...

How to modify the default submission method of the form

The default submission method of html is get inste...

Vue.js implements simple timer function

This article example shares the specific code of ...

A brief analysis of crontab task scheduling in Linux

1. Create a scheduling task instruction crontab -...

MySQL 4 methods to import data

1. Import mysql command The mysql command import ...