六、…
…叫拓展符号,其作用是把数组解开成单独的元素
//eg1:
let list = [1, 2, 3]
console.log(...list) //输出打印=>1 2 3
//eg2:
let oneList = [1, 2, 3, 4]
let towList = [...oneList, 5, 6]
console.log(towList) //输出打印=>[1, 2, 3, 4, 5, 6]
七、解构
//eg1:
let [a, b] = ['Jason', 'Just']
[a, b] = [b, a]
console.log(a,b) //打印输出=>Just Jason
//eg2:
const numOld = function(num, ...arr) {
console.log(num, arr, arr.length)
}
const numNew = function() {
numOld(1, 2, 3, 4)
}
numNew() //输出打印=> 1 [2, 3, 4] 3
详细解说点击链接查看=>es6解构
八、箭头函数
1、箭头函数可以称为是匿名函数定义的简化版,语法如下:
(参数1, 参数2) => {语法}
(参数1, 参数 2) => 语句
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓(等同于)
function(参数1, 参数2) {
return 语句
}
2、如果只有一个参数,可去掉小括号,语法如下:
(参数1) => { 语句 }
参数1 => { 语句 }
参数1 => 语句
3、如果没有参数,小括号必写,语法如下:
() => { 语句 }
案例:
let arr = [1, 2, 3]
let oneList = arr.map(function(n) {
return n * n
})
let twoList = arr.map(n => n * n)
console.log(oneList) //输出打印=>[1, 4, 9]
console.log(twoList) //输出打印=>[1, 4, 9]
九、遍历数组
1、普通遍历,写法如下:
let arr = [1, 2, 3, 4]
for (let i = 0; i < arr.length; i++) {
let num = arr[i]
console.log(num)
}
//输出打印=> 1 2 3 4
2、es6遍历,写法如下:
let arr = [1, 2, 3, 4]
for(let num of arr) {
console.log(num)
}
//输出打印=> 1 2 3 4
3、遍历对象,写法如下:
let person = {
name: 'Jaosn',
age: 18
}
for(let p of person) {
console.log(p, person[p])
}
//控制台输出打印=> name Jason
// => age 18