目录
1.扩展运算符...
//剩余运算符 允许把多个独立的参数合并到一个数组中 一般用在函数的形参上
//扩展运算符 将一个数组分割,将各个项作为分离的参数传给函数
// const maxNum = Math.max(20, 30);
// console.log(maxNum);
es5
//1.处理数组中的,使用apply
const arr = [10, 2, 3, 4, 51, 4, 5, 6];
console.log(Math.max.apply(null, arr));
// 等价于Math.max(10, 2, 3, 4, 51, 4, 5, 6)
// 第一个参数表示max中最大的一个数,this
// 第二个作为参数列表不再是数组,传递给求最大数的函数
es6使用扩展运算符
console.log(Math.max(...arr));
箭头函数
1.
// function add(a,b){
// return a+b;
// }
// 2.箭头函数 (参数)=>{}
// let add = (a, b) => {
// return a + b;
// }
// 3、如果函数体中只有一条return语句可以省略花括号
let add = (a, b) => a + b
console.log(add(10, 20));
4.对象
// let obj = function(id) {
// let person = {
// id: id,
// name: 'xiaoma'
// }
// return person
// }
// 返回值是一个对象或者数组一定要用小括号抱起来
let obj = id => ({
id: id,
name: 'xiaoma'
})
console.log(obj(2));
5.闭包函数
// let fn1 = (function() {
// return function() {
// console.log('helloya');
// }
// })();//闭包函数
let fn = (() => {
return ()=>{console.log(222)}
})()
不是return 语句就要写花括号,只有一条return语句时可以省略花括号,也可以省略小括号(当返回对象或者数组的时候加())