zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    Python合集之Python循环语句(二)
    io流2
    io流
    集合工具类
    泛型
    Map
    VSCode_Extensions
    C++ in VSCode
    C# 私有字段前缀 _ 的设置(VS2019, .editorconfig)
    dotnet 跨平台编译发布
  • 原文地址:https://www.cnblogs.com/ronle/p/11488558.html
Copyright © 2011-2022 走看看