Avoiding Problems Caused by Closures in JavaScript

Avoiding Problems Caused by Closures in JavaScript

About let to avoid problems caused by closure

Use object-oriented thinking to complete the buyer information deletion function. Each piece of information contains:

Name Phone Number Province

Implement the following requirements:
No third-party libraries can be used and native code must be used.
Combined with the basic code structure given, add code at the two locations below code here to complete the buyer information deletion function. Note that this page must be displayed clearly on the mobile phone.

The js code can be adjusted arbitrarily, for example, and completed using es6 code.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <!--code here-->
    <title>demo</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .head, li div {
            display: inline-block;
            width: 70px;
            text-align: center;
        }

        li .id, li .sex, .id, .sex {
            width: 15px;
        }

        li .name, .name {
            width: 40px;
        }

        li .tel, .tel {
            width: 90px;
        }

        li .del, .del {
            width: 15px;
        }

        ul {
            list-style: none;
        }

        .user-delete {
            cursor: pointer;
        }

    </style>
</head>

<body>
<div id="J_container">
    <div class="record-head">
        <div class="head id">Serial number</div>
        <div class="head name">Name</div>
        <div class="head sex">Gender</div>
        <div class="head tel">Phone number</div>
        <div class="head province">Province</div>
        <div class="head">Operation</div>
    </div>
    <ul id="J_List">
        <li>
            <div class="id">1</div>
            <div class="name">Zhang San</div>
            <div class="sex">Male</div>
            <div class="tel">13788888888</div>
            <div class="province">Zhejiang</div>
            <div class="user-delete">Delete</div>
        </li>
        <li>
            <div class="id">2</div>
            <div class="name">Li Si</div>
            <div class="sex">female</div>
            <div class="tel">13788887777</div>
            <div class="province">Sichuan</div>
            <div class="user-delete">Delete</div>
        </li>
        <li>
            <div class="id">3</div>
            <div class="name">Wang Er</div>
            <div class="sex">Male</div>
            <div class="tel">13788889999</div>
            <div class="province">Guangdong</div>
            <div class="user-delete">Delete</div>
        </li>
    </ul>
</div>

<script>
    // You can also use ES6 here function Contact() {
        this.init();
    }

    // your code here
</script>
</body>
</html>

code1

<meta name="viewport" content="width = device-width,initial-scale=1">

code2 (other people's code)

 Contact.prototype.init = function () {
        console.log("Test");
        var div = document.getElementsByClassName("user-delete");
        var ul = document.querySelector("#J_List");
        var list = ul.querySelectorAll("li");

        for (var i = 0; i < div.length; i++) {
            (function (i) {
                div[i].onclick = function () {
                    list[i].remove();
                    console.log(i);
                }
            })(i);
        }
    }

    new Contact();

in

 (function (i) {
                div[i].onclick = function () {
                    list[i].remove();
                    console.log(i);
                }
            })(i);

I don't understand the meaning of this immediate execution function

My code

 Contact.prototype.init = function () {
        let div = document.getElementsByClassName("user-delete");
        let ul = document.querySelector("#J_List");
        let list = ul.querySelectorAll("li");

        for (let i in div) {
            div[i].onclick = function () {
                list[i].remove();
                console.log(i);
            }
        }
    }

    new Contact();

Later I remembered that it was to avoid the problems caused by closures. Teacher Liao Xuefeng talked about this, but I didn’t remember it at the moment. For details, please see Liao Xuefeng’s closure. However, there was no problem running my code because there was no block-level scope at the time. But now I can use let to avoid this problem. So if i is declared using let, there is no need to execute the function immediately. And when writing code, you should avoid using var and use let instead. Another thing is to avoid using statements like for(let i =0;condition;++i) and try to use for...in... Some good habits should be developed.

This concludes this article on how to avoid problems caused by closures with let in JavaScript. For more information on let closures in JavaScript, please search previous articles on 123WORDPRESS.COM or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Function nesting in JavaScript
  • Learn about JavaScript closure functions in one article
  • JavaScript Closures Explained
  • Javascript scope and closure details
  • JS Difficulties Synchronous and Asynchronous and Scope and Closure and Detailed Explanation of Prototype and Prototype Chain
  • Detailed explanation of JavaScript closure issues
  • Detailed explanation of JavaScript function usage [function definition, parameters, binding, scope, closure, etc.]
  • Details of function nesting and closures in js

<<:  Graphical introduction to the difference between := and = in MySQL

>>:  Docker compose custom network to achieve fixed container IP address

Recommend

Detailed explanation of Apache website service configuration based on Linux

As an open source software, Apache is one of the ...

Analysis of MySql index usage strategy

MySql Index Index advantages 1. You can ensure th...

What is TypeScript?

Table of contents 1. JavaScript issues 2. Advanta...

Detailed steps to install MySQL 8.0.27 in Linux 7.6 binary

Table of contents 1. Environmental Preparation 1....

Why is the scroll bar on the web page set on the right?

Why are the scroll bars of the browsers and word ...

Tutorial on deploying jdk and tomcat on centos7 without interface

1. Install xshell6 2. Create a server connection ...

Detailed explanation of HTML document types

Mine is: <!DOCTYPE html> Blog Garden: <!...

Problems with nodejs + koa + typescript integration and automatic restart

Table of contents Version Notes Create a project ...

Steps for Vue to use Ref to get components across levels

Vue uses Ref to get component instances across le...

How to use the Linux more command in Linux common commands

more is one of our most commonly used tools. The ...

Detailed explanation of MySQL solution to USE DB congestion

When we encounter a fault, we often think about h...

Detailed explanation of mysql integrity constraints example

This article describes the MySQL integrity constr...

Installation tutorial of MySQL 5.7.17 zip package version under win10

The installation tutorial of mysql5.7.17 is share...