二、变量的解构赋值
数组的解构
const F4=['小沈阳','刘能','赵四','宋小宝'];
let[xiao,liu,zhao,song]=F4;
console.log(xiao);
输出 小沈阳。
对象的解构
const zhao={
name:"赵本山",
age:"不详",
xiaopin:function(){
console.log("我可以演小品");
}
}
let {name,age,xiaopin}=zhao;
console.log("name");
//如果常使用函数xiaopin
let{xiaopin}=zhao;
xiaopin();//可以不写成zhao.xiaopin();
三、模板字符串
ES6引用新的字符串符号 ——反引号
使用反引号,内容中可直接出现换行符,单双引号里不能出现换行。
let lovest="玛丽";
let out=`${lovest}是我最喜欢的演员!`//代替了原来的+号
四、对象的简化写法
let name="葫芦娃";
let change=function(){
console.log("改变世界!");
}
//添加两个属性,属性名和变量名相同的
const school={
name,
change//省略function
improve(){//省略function
console.log("我们会变得更好!")
}
}
五、箭头函数及其声明特点
let fn=function(a,b){
}//可以被代替为
let fn=(a,b)=>{
return a+b;
}
fn(1,2);
特点:1.this是静态的,this始终指向函数声明所在作用域下的this的值。
不能作为构造实例化对象
3.不能使用argument变量
箭头函数的应用:
//案例一:2秒后变粉色
//获取元素
let ad=document.getElementById('ad');
//绑定元素
ad.addEventListener('click',function(){
// let _this=this;//保存this的值,用箭头函数可去掉这一步
//定时器
setTimeout(function(){
_this.style.background='pink';
},2000)
})
//案例二:
const arr=[1,2,3,4,5];
// const result=arr.filter(function(item){
// if(item%2===0){
// return true;
// }else{
// return false;
// }
// })
const result=arr.filter(item=>item%2===0);
console.log(result);
箭头函数适合与this无关的回调,如定时器和数组的回调。
箭头函数不适合与this有关的回调,如事件回调,对象的方法。