search
HomeWeb Front-endJS TutorialDiscussion on the problem of javascript self-starting function_javascript skills

Not much to say.

Let’s look at two pieces of code first:

Copy the code The code is as follows:

var elems = document.getElementsByTagName('a');

for (var i = 0; i
alert(i);
elems[i].addEventListener('click', function (e) {
e.preventDefault();
alert('I am link #' i);
}, 'false');
}

Look at it again:
Copy the code The code is as follows:

var elems = document.getElementsByTagName('a');

for (var i = 0; i
(function(index ){
elems[i].addEventListener('click', function (e) {
e.preventDefault();
alert('I am link #' index);
}, ' false');
})(i);
}

HTML code is as follows:
Copy code The code is as follows:


a
a
a
a
a
a
a
a


You can imagine the effect of the two script codes before and after.

If you can see the difference in effect, congratulations. At least I thought about it for a long time before I understood the mystery behind it.

Yes. You read that right, the first piece of code here, no matter which link you click, the output is I am link # 8.

The second piece of code is the result you really want, then Why.

Look at the code below:
Copy the code The code is as follows:

var elems = document.getElementsByTagName('a');

for (var i = 0; i
alert(i);
elems[ i].addEventListener('click', function (e) {
e.preventDefault();
alert('I am link #' i);
//Note that the callback function here only triggers
//Same, the value of i here also changes at the end of the loop
}, 'false');

//The reason is
//Although elems[i] here is a referenced element
//but i in the callback function has changed to 8 after the loop ends
//(if the length of elems is 8)
}

Copy code The code is as follows:

var elems = document. getElementsByTagName('a');

for (var i = 0; i
(function(index){
elems[i]. addEventListener('click', function (e) {
e.preventDefault();
alert('I am link #' index);
}, 'false');
})( i);
//This is different
//Although the value of i becomes 8 after the loop ends
//But the index encapsulated in the closure is indeed locked.
//Always saved in memory.
//To be precise, the entire function should be locked in memory.

}

Some knowledge of javascript closures may be required here.

I have thought about the above code for a long time and recorded it to prevent forgetting.
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
提高 Python 代码可读性的五个基本技巧提高 Python 代码可读性的五个基本技巧Apr 12, 2023 pm 08:58 PM

Python 中有许多方法可以帮助我们理解代码的内部工作原理,良好的编程习惯,可以使我们的工作事半功倍!例如,我们最终可能会得到看起来很像下图中的代码。虽然不是最糟糕的,但是,我们需要扩展一些事情,例如:load_las_file 函数中的 f 和 d 代表什么?为什么我们要在 clay 函数中检查结果?这些函数需要什么类型?Floats? DataFrames?在本文中,我们将着重讨论如何通过文档、提示输入和正确的变量名称来提高应用程序/脚本的可读性的五个基本技巧。1. Comments我们可

CRPS:贝叶斯机器学习模型的评分函数CRPS:贝叶斯机器学习模型的评分函数Apr 12, 2023 am 11:07 AM

连续分级概率评分(Continuous Ranked Probability Score, CRPS)或“连续概率排位分数”是一个函数或统计量,可以将分布预测与真实值进行比较。机器学习工作流程的一个重要部分是模型评估。这个过程本身可以被认为是常识:将数据分成训练集和测试集,在训练集上训练模型,并使用评分函数评估其在测试集上的性能。评分函数(或度量)是将真实值及其预测映射到一个单一且可比较的值 [1]。例如,对于连续预测可以使用 RMSE、MAE、MAPE 或 R 平方等评分函数。如果预测不是逐点

详解JavaScript函数如何实现可变参数?(总结分享)详解JavaScript函数如何实现可变参数?(总结分享)Aug 04, 2022 pm 02:35 PM

js是弱类型语言,不能像C#那样使用param关键字来声明形参是一个可变参数。那么js中,如何实现这种可变参数呢?下面本篇文章就来聊聊JavaScript函数可变参数的实现方法,希望对大家有所帮助!

盘点Python内置函数sorted()高级用法实战盘点Python内置函数sorted()高级用法实战May 13, 2023 am 10:34 AM

一、前言前几天在Python钻石交流群有个叫【emerson】的粉丝问了一个Python排序的问题,这里拿出来给大家分享下,一起学习下。其实这里【瑜亮老师】、【布达佩斯的永恒】等人讲了很多,只不过对于基础不太好的小伙伴们来说,还是有点难的。不过在实际应用中内置函数sorted()用的还是蛮多的,这里也单独拿出来讲一下,希望下次再有小伙伴遇到的时候,可以不慌。二、基础用法内置函数sorted()可以用来做排序,基础的用法很简单,看个例子,如下所示。lst=[3,28,18,29,2,5,88

如何在Linux系统中使用Systemd和Crontab实现系统自启动如何在Linux系统中使用Systemd和Crontab实现系统自启动Sep 26, 2023 am 11:57 AM

如何在Linux系统中使用Systemd和Crontab实现系统自启动引言:在Linux系统中,我们经常需要将一些常用的服务或脚本设置为系统自启动,以便系统重启后能够自动运行。在本文中,将介绍如何使用Systemd和Crontab这两个工具来实现系统自启动,并给出具体的代码示例。一、Systemd的使用Systemd是Linux操作系统中常用的系统和服务管理

学Python,还不知道main函数吗学Python,还不知道main函数吗Apr 12, 2023 pm 02:58 PM

Python 中的 main 函数充当程序的执行点,在 Python 编程中定义 main 函数是启动程序执行的必要条件,不过它仅在程序直接运行时才执行,而在作为模块导入时不会执行。要了解有关 Python main 函数的更多信息,我们将从如下几点逐步学习:什么是 Python 函数Python 中 main 函数的功能是什么一个基本的 Python main() 是怎样的Python 执行模式Let’s get started什么是 Python 函数相信很多小伙伴对函数都不陌生了,函数是可

Python面向对象里常见的内置成员介绍Python面向对象里常见的内置成员介绍Apr 12, 2023 am 09:10 AM

好嘞,今天我们继续剖析下Python里的类。[[441842]]先前我们定义类的时候,使用到了构造函数,在Python里的构造函数书写比较特殊,他是一个特殊的函数__init__,其实在类里,除了构造函数还有很多其他格式为__XXX__的函数,另外也有一些__xx__的属性。下面我们一一说下:构造函数Python里所有类的构造函数都是__init__,其中根据我们的需求,构造函数又分为有参构造函数和无惨构造函数。如果当前没有定义构造函数,那么系统会自动生成一个无参空的构造函数。例如:在有继承关系

win10带盾牌的软件怎么自启动win10带盾牌的软件怎么自启动Feb 14, 2024 pm 09:03 PM

所有具有EXE文件拓展名的应用程序都会在其快捷方式图标上方添加一枚小盾牌作为标记,每次启动这些应用程序时,屏幕将会弹窗警告并要求用户加以确认才能继续运行,我们怎么做才能让他自启动呢。win10带盾牌的软件怎么自启动1、首先我们右键点击开始菜单然后选择运行,当然我们也可以按下win+R快捷键直接打开运行弹窗,我们在这里可以输入gpedit.msc。2、随后我们进入到本地组策略编辑器界面,我们在这里选择计算机配置,然后点击windows设置,进入以后在点击安全设置,在这里选择本地策略中的安全选项。3

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool