ES6-解构表达式
1】数组解析
let arr = [1,2,3]; //以前我们想获取其中的值,只能通过角标 // let a = arr[0] // let b = arr[1] // let c = arr[2]; //ES6可以这样,a,b,c 将与arr中每个位置对应来取值 let [a,b,c] = arr;
//用一个变量代替数组下标索引位置的元素‘ console.log(a,b,c)
2】对象解构
const person = { name: "jack", age: 21, language: ['java', 'js', 'css'] } //解构表达式获取值,将person里面的每个属性和左边对应赋值 //const { name, age, language } = person; //等于下面 //以前的写法 // const name = person.name; // const age = person.age; // const language = person.language; //或者将name值给abc const { name: abc, age, language } = person; console.log(abc, age, language) //console.log(name, age, language)
3】箭头函数解构表达式运用
//以前 function hello(person) { console.log("hello" + person.name) } hello(person); //helloqiyue //箭头函数 let hello2 = params => console.log("hello" + person.name) hello2(person) //helloqiyue //箭头函数加解构表达式 //用一个参数代替对象里的属性,来直接用其属性取代对象点属性名 var hello3 = ({ name }) => console.log("hello" + name) hello3(person) //helloqiyue