解构赋值
ES6中允许从数组中提取值,按照对应位置,对变量赋值。对象也可以实现解构。
按照一定模式,从数组中或者对象中提取值,将提取出来的值赋值给另外的变量。
- 数组解构
let [a,b,c] = [1,2,3];
console.log(a);
console.log(b);
console.log(c);
//如果解构不成功,变量的值为undefined。
let [foo] = [];
let [bar, foo] = [1];
- 对象解构
let person = {name:"Lee",age:20};
let {name , age} = person;
let {name:myName,age:myAge} = person // myName... 属于别名
console.log(myName); //"Lee"
console.log(myAge); //20
箭头函数
- ES6中新增的函数定义方法
()=>{}
const fn = ()=>{}
- 函数体中只有一句代码,且执行结果就是返回值,可以省略大括号
function sum(target,len) {
return target+len;
}
const sum = (target,len)=>target+len;
- 如果形参只有一个,可以省略小括号
const fn = a =>{
alert(a)
}
- 箭头函数不绑定this关键字,箭头函数中的this,指向的是函数定义位置的上下文this。
const obj = {name:"Lee"};
function fn() {
console.log(this);
return ()=>{
console.log(this);
};
}
const resFn = fn.call(obj);
resFn();