记 ()=>{}箭头函数的使用

发布时间:2022-06-29 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了记 ()=>{}箭头函数的使用脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
let  foo3 = function() {
	console.log('hahawoshi foo3')
}
foo3();

let foo4 = () => {
	console.log('haha woshi foo4')
}
foo4();

//箭头函数this 是静态的,this始终指向函数声明时所在的作用域下的this 的值

//call 能改变this的指向

function getName() {
	console.log(this.name);
}

let getName2 = () => {
	console.log(this.name);
}

window.name = '哈哈';

let people = {
	name: '张三',
	age : 18
	
}
getName();//哈哈

getName2();//哈哈

getName.call(people);//张三  普通函数 this指向当前声明的函数
getName2.call(people);//哈哈 箭头函数 this 指向函数声明时所在作用域下的this的值2.不能作为构造实例化对象3.不能使用arguments变量

let f = () => { console.log(arguments);}f(1);//报错let f2 = function() { console.log(arguments);}f2(1);//arguments...

let arrx = [1,66,54,78,66,3,1]

const res5 = arrx.filter(function(item) { if(item % 2 === 0) { return true; } else { return false; }})

res6 = arrx.filter(item => { if(item%2===0) { return true; } else { return false; }})

res6 = arrx.filter(item => item % 2 ===0)console.log(res6,'res6');

 

箭头函数适合与this 无关的回调,定时器,数组的方法回调

箭头函数不适合与this 有关的回调,事件回调

  

脚本宝典总结

以上是脚本宝典为你收集整理的记 ()=>{}箭头函数的使用全部内容,希望文章能够帮你解决记 ()=>{}箭头函数的使用所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: