箭头函数带来的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后的运行结果

相关内容

热门资讯

【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...