ES6(六)函数扩展

函数扩展

// 函数默认值
const test = (x, y = 'world') => {
  console.log(x, y)
}
// hello world
test('hello')
// hello ronle
test('hello', 'ronle')


// 作用域
let x = 'demo'
const test = (x, y = x) => {
  console.log(x, y)
}
// demo demo
test(x)
// hello hello
test('hello')


// 不确定参数个数
const test = (...arg) => {
  for (let v of arg) {
    console.log(v)
  }
}
test(1, 2, 3, 4, 'a')


const test = (x, ...arg) => {
  console.log(x)
  for (let v of arg) {
    console.log(v)
  }
}
test(1, 2, 3, 4, 'a')





展开运算符

// 1 2 3 4 5 6 7
console.log(...[1, 2, 3, 4, 5, 6, 7])
// a 1 2 3 4 5 6 7
console.log('a', ...[1, 2, 3, 4, 5, 6, 7])


箭头函数

const arr = v => v * 2
const arr2 = () => 10
// 6
console.log(arr(3))
// 10
console.log(arr2())

// 尾调用(某个函数的最后一步是调用另一个函数)
const run = (x) => console.log(x)
const person = (x) => {
  return run(x)
}
person(123)

 

posted @ 2019-09-08 21:20  ronle  阅读(231)  评论(0编辑  收藏  举报