ES6 Arrow Function & this bug
ES6 Arrow Function & this bug

let accHeadings = document.querySelectorAll(`.accordionItemHeading`); // NodeList(3)
// let accHeadings = [...document.querySelectorAll(`.accordionItemHeading`)];
for (let i = 0; i < accHeadings.length; i++) {
accHeadings[i].addEventListener("click", function(e) {
console.log(`this`, this);
console.log(`e.target`, e.target);
});
}
for (let i = 0; i < accHeadings.length; i++) {
// ES6 Arrow Function & this bug
accHeadings[i].addEventListener("click", () => {
console.log(`this`, this);
}, false);
}

<!DOCTYPE html>
<html lang="zh-Hans">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="author" content="xgqfrms">
<meta name="generator" content="VS code">
<title>ES6 Arrow Function & this bug</title>
</head>
<body>
<section>
<h1>ES6 Arrow Function & this bug</h1>
</section>
<section>
<h2 class="accordionItemHeading">About accordions</h2>
<h2 class="accordionItemHeading">Accordion items</h2>
<h2 class="accordionItemHeading">How to use a JavaScript accordion</h2>
</section>
<section>
<button class="btn">a</button>
<button class="btn">b</button>
<button class="btn">c</button>
</section>
<!-- js -->
<script>
let btns = [...document.querySelectorAll(`.btn`)];
btns.forEach((acc) => {
acc.addEventListener("click", function(e) {
console.log(`this`, this);
console.log(`e.target`, e.target);
});
});
let accHeadings = [...document.querySelectorAll(`.accordionItemHeading`)];
// for (let i = 0; i < accHeadings.length; i++) {
// accHeadings[i].addEventListener("click", function(e) {
// console.log(`this`, this);
// console.log(`e.target`, e.target);
// });
// }
for (let i = 0; i < accHeadings.length; i++) {
// ES6 Arrow Function & this bug
accHeadings[i].addEventListener("click", (e) => {
console.log(`this`, this);
console.log(`e.target`, e.target);
});
}
</script>
</body>
</html>
ES6 Arrow Function & this bug的更多相关文章
- vue & lifecycle methods & this bug & ES6 Arrow function & this bind bug
vue & lifecycle methods & this bug ES6 Arrow function & this bind bug bad fetchTableData ...
- ES6 Arrow Function All In One
ES6 Arrow Function All In One this const log = console.log; const arrow_func = (args) => log(`arg ...
- ES6 arrow function vs ES5 function
ES6 arrow function vs ES5 function ES6 arrow function 与 ES5 function 区别 this refs xgqfrms 2012-2020 ...
- ES6 Arrow Function return Object
ES6 Arrow Function return Object https://github.com/lydiahallie/javascript-questions/issues/220#issu ...
- ES6 arrow function
语法: () => { … } // 零个参数用 () 表示: x => { … } // 一个参数可以省略 (): (x, y) => { … } // 多参数不能省略 (): 当 ...
- [ES6] 06. Arrow Function =>
ES6 arrow function is somehow like CoffeeScirpt. CoffeeScript: //function call coffee = -> coffee ...
- [ES6系列-02]Arrow Function:Whats this?(箭头函数及它的this及其它)
[原创] 码路工人 大家好,这里是码路工人有力量,我是码路工人,你们是力量. 如果没用过CSharp的lambda 表达式,也没有了解过ES6,那第一眼看到这样代码什么感觉? /* eg.0 * fu ...
- vue watch & arrow function bug
vue watch & arrow function bug watch: { GeoJSON: function(newValue, oldValue) { log(`\n\n\nGeoJS ...
- ES6 new syntax of Arrow Function
Arrow Function.md Arrow Functions The basic syntax of an arrow function is as follows var fn = data ...
随机推荐
- 【extjs6学习笔记】0.0 准备
1.下载ExtJS 6 下面是Ext JS 6正式版的GPL版本下载地址 https://www.sencha.com/legal/gpl/ 2.下载sencha cmd 安装完成后,命令行运行出现以 ...
- 基于phpExcel写的excel类(导出为Excel)
<?php /* * 类的功能 * 传入二位数组导出excel * 传入excel 导出二位数组 * @author mrwu */ require('PHPExcel.php'); requi ...
- Azure 项目构建 - 构建 WordPress 网站
本课程主要介绍了如何基于 Azure Web 应用和 WordPress 快速构建网站,实践讲解如何使用 Azure Web 应用,创建并连接 MySQL Database on Azure, 使用 ...
- PostgreSQL 的日期函数用法举例
最近偶有开发同事咨询 PostgreSQL 日期函数,对日期处理不太熟悉,今天详细看了下手册的日期函数,整理如下,供参考. 一 取当前日期的函数 --取当前时间skytf=> select no ...
- VirtualKD + Windbg 调试Win10虚拟机
安装完vminstall后,先在运行中输入"msconfig"命令,显示如下窗口. 首先点击“引导”选项卡,然后选择最后一个引导项(Disable Signature Enforc ...
- 查看Windows激活信息
使用 Windows + R组合快捷键打开运行命令框 1.运行: slmgr.vbs -dlv 可以查询到Win10的激活信息,包括:激活ID.安装ID.激活截止日期等信息. 2.运行: slmgr. ...
- leetcode_1052. Grumpy Bookstore Owner
1052. Grumpy Bookstore Owner https://leetcode.com/problems/grumpy-bookstore-owner/ 题意:每个时刻i会有custome ...
- C++数据文件存储与加载(利用opencv)
首先请先确认已经安装好了opencv3及以上版本. #include <opencv2/opencv.hpp>#include <iostream>#include <s ...
- nyoj-1103-区域赛系列一多边形划分
http://acm.nyist.net/JudgeOnline/problem.php?pid=1103 区域赛系列一多边形划分 时间限制:1000 ms | 内存限制:65535 KB 难度: ...
- 洛谷 P1120 小木棍[数据加强版]
这道题可能是我做过的数据最不水的一道题-- 题目传送门 这题可以说是神剪枝,本身搜索并不算难,但剪枝是真不好想(好吧,我承认我看了题解)-- 剪枝: 用桶来存储木棍 在输入的时候记录下最长的木棍和最短 ...