箭头函数带来的this变化实例
创始人
2024-05-05 06:39:00
0次

1.不使用箭头函数时

 let Lesson = {site: '后盾人',lists:['js','css','mysql'],show: function (param) { console.log(this);// {site: '后盾人', lists: Array(3), show: ƒ}return this.lists.map(function(title){console.log(this);// Window {window: Window, self: Window, document: document, name: '', location: Location, …}})}}Lesson.show()

运行结果
第一个console:直接挂载到对象属性上的函数,this指向所属对象
第二个console:未直接挂载到对象属性上的函数,this指向window

2.使用箭头函数

// 2.使用this{let Lesson = {site: '后盾人',lists:['js','css','mysql'],show: function (param) { console.log(this);// {site: '后盾人', lists: Array(3), show: ƒ}return this.lists.map((title)=>{console.log(this);// {site: '后盾人', lists: Array(3), show: ƒ}})}}Lesson.show()}

运行结果
map中改用箭头函数后,this指向了上下文,也就是父级作用域,即这里的Lesson对象。

3.给dom元素添加事件

{let Dom = {site: '后盾人',bind: function() {const button = document.querySelector('button')button.addEventListener("click",function(){console.log(this)})button.onclick = function(){console.log(this)}/*** 这两种写法的效果是一样的,this都指向button对象* 可以理解为onclick这个函数挂载到button对象上的一个属性* 上文提到,直接挂载到对象属性上的函数,this指向所属对象*/}}Dom.bind()}

运行结果
两处this均指向button对象

// 改成箭头函数后{let Dom = {site: '后盾人',bind: function() {const button = document.querySelector('button')button.addEventListener("click",()=>{console.log(this)})                }}Dom.bind()}

运行结果
改成箭头函数后,this指向父级作用域中的this,即bind函数中的this,而bind函数是挂载到Dom对象上的,因此this指向Dom

4.思考:上面代码中,既要用到button对象,又要用到Dom对象怎么办?

1.使用evenet参数

 // 使用event参数{let Dom = {site: '后盾人',bind: function() {const button = document.querySelector('button')button.addEventListener("click",event=>{console.log(this)console.log(event.target)})                }}Dom.bind()}

运行结果
此时,this指向Dom对象,event.target指向button对象

3.另外定义一个变量记录Dom作用域的this

// 6 定义self{let Dom = {site: '后盾人',bind: function() {const self = thisconst button = document.querySelector('button')button.addEventListener("click",function(){console.log(this)console.log(self)})                }}Dom.bind()}

运行结果
此时也能同时拿到Dom对象和button对象

4.使用handleEvent

// 使用handleEvent{let Dom = {site: '后盾人',handleEvent: function (event) { console.log(event.target)console.log(this)},bind: function() {const button = document.querySelector('button')// 事件会自动在传入对象中寻找handleEvent方法button.addEventListener("click",this)   }}Dom.bind()}

运行结果

5.思考:上面代码中,场景变成两个button,既要用到点击的button对象,又要用到Dom对象怎么办?

 {let Dom = {site: '后盾人',bind: function() {// 此时会拿到两个buttonlet buttons = document.querySelectorAll('button')buttons.forEach(function(elem) {elem.addEventListener('click',function() {// 此时this指向各自点击的button对象console.log(this)})})                        }}Dom.bind()}

点击各自button后的运行结果

使用箭头函数后

//  两个button时{let Dom = {site: '后盾人',bind: function() {// 此时会拿到两个buttonlet buttons = document.querySelectorAll('button')buttons.forEach((elem) =>{console.log(this)//指向Dom对象elem.addEventListener('click',()=> {// 此时this指向各自点击的button对象console.log(this)//指向Dom对象})})                        }}Dom.bind()}

点击后的运行结果
此时this指向了Dom
再加上event参数

{let Dom = {site: '后盾人',bind: function() {// 此时会拿到两个buttonlet buttons = document.querySelectorAll('button')buttons.forEach((elem) =>{console.log(this)//指向Dom对象elem.addEventListener('click',(event)=> {// 此时this指向各自点击的button对象console.log(this)//指向Dom对象console.log(event.target)//指向点击的button对象})})                        }}Dom.bind()}

点击各自的button后的运行结果

相关内容

热门资讯

前端-session、jwt 目录:   (1)session (2&#x...
linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
nodejs:本地安装nvm实... 一、背景-使用不同版本node的原因 vue3+ts、nuxt3版本,node...
JAVA集合知识整理 Java集合知识整理 HashMap相关 HashMap的底层数据结构:jdk1.8之...
无刷直流电机介绍及单片机控制实... 无刷直流电机介绍及单片机控制实例前言基本概念优势与劣势使用寿命基本结构使用单片机控制实例电子调速器&...
fwdiary(2) dp2 1.传纸条  AcWing 275. 传纸条 - AcWing 走两条路,走一条最大的...
常用的DOS命令 常用的DOS命令 DOS(Disk Operating System,磁...
<C++> 类和对象(下) 1.const成员函数将const修饰的“成员函数”称之为const成员函数,cons...