<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.box1{
width: 500px;
height: 500px;
background-color:blueviolet;
}
</style>
</head>
<body>
<div id="box1" class="box1"></div>
<script type="text/javascript">
/*
箭头函数适合应用的场景是与this无关的场景,定时器(定时器我认为是因为js的执行顺序,他会先初始化代码,
他后添加计时器,所以如果不使用箭头函数下的this他可能是会指向window,所以可以先用变量存储this然后再引用
),数组
箭头函数最不合适作为回调函数本体,他因为this指向是声明后不可改动的所以,他可能会指向window,而不是事件源,所以也不太适合于作为对象内的方法
*/
let box1=document.getElementById("box1");
box1.addEventListener("click",function(){//addEventListener是可以为所有dom元素添加事件且可以添加多个事件,第一个参数是事件行为,
//第二个参数是回调函数,事件触发后的逻辑
/// let _this=this;
setTimeout(//function(){
//console.log(this);Window {window: Window, self: Window, document: document, name: '', location: Location, …}
//console.log(_this);box1的dom
//}
()=>{
this.style.backgroundColor="pink";
}
,2000);
})
/数组中的应用
let myArr=[5,2,7,22,8,9,50];//let最大特性不会变量提升和单一存在和可划分
//const需要保证变量地址不改变
const nowArr=myArr.filter((ct)=>ct%2 === 0); //filter应该是一个数组的方法
// 他会遍历整个数组(每个参数都会以形参的方法传入规则内)以括号内的规则 来判断是否为 true 是true就会整合以数组方式返回
console.log(nowArr);
</script>
</body>
</html>
es6箭头箭头函数的应用与数组的filter方法
于 2022-02-04 13:02:18 首次发布