zoukankan      html  css  js  c++  java
  • 009--函数(基本实例和函数类型)

    基本函数

    function  add(x, y) {
      return x + y
    }
    
    let myAdd = function(x, y) {
      return x + y
    }
    
    let z = 100
    function addToZ(x, y) {
      return x + y + z
    }

     给刚才的函数添加完整的函数类型

    //给刚才的函数添加类型
    function  add(x: number, y: number): number {
      return x + y
    }
    //给函数添加完整的函数类型
    let myAdd:(baseValue: number, increment: number)=> number = function(x: number, y: number): number {
      return x + y
    }

    函数类型推断

    //给函数添加完整的函数类型
    let myAdd:(baseValue: number, increment: number)=> number = function(x: number, y: number): number {
      return x + y
    }
    //上述函数可以拆分成下面两种
    // let myAdd = function(x: number, y: number): number {
    //   return x + y
    // }
    // let myAdd:(baseValue: number, increment: number)=> number = function(x, y) {
    //   return x + y
    // }
    //都会根据类型推断来推断出参数类型和返回值类型

    可选参数

    //在TypeScript中的参数只要定义了就是必须的,js中默认都是可选的
    function bulidName(firstName: string, lastName: string): string {
      return firstName + '' + lastName 
    }
    
    // let result1 = bulidName('Bob')//报错,只传了一个参数
    // let result2 = bulidName('Bob', 'Jack', 'Lisa')//报错,传入了三个参数
    let result3 = bulidName('Bob', 'Jack')
    //改造一下,让第二个参数可选
    function bulidName(firstName: string, lastName?: string): string {
      if(lastName) {
      return firstName + '' + lastName 
      }else {
        return firstName
      }
    }
    let result = bulidName('Bob')
    //可选参数,必须跟在必须参数的后面 

    函数默认值

    //函数默认值
    function bulidName(firstName: string, lastName = 'Smith'): string {
      return firstName + '' + lastName 
    }
    
    let result = bulidName('Bob')
    console.log(result)//BobSmith
    //函数默认值
    function bulidName(firstName = 'Bob', lastName: string ): string {
      return firstName + '' + lastName 
    }
    
    let result = bulidName('Jack', 'smith')
    let result1 = bulidName(undefined, 'Jons')
    console.log(result)//Jacksmith
    console.log(result1)//BobJons
    //如果想把默认参数放在前面,使用函数默认值的值的话则在函数调用时一定要在相应位置传入undefined

    剩余参数

    function bulidName(firstName = 'Bob', ...restOfName: string[] ): string {
      return firstName + '' + restOfName
    }
    
    let result = bulidName('Jack', 'smith')
    let result1 = bulidName(undefined, 'Jons')
    let result3 = bulidName('Jack', 'smith', 'Niclos')
    console.log(result)//Jacksmith
    console.log(result1)//BobJons
    console.log(result3)//Jacksmith,Niclos
    //如果想把默认参数放在前面,使用函数默认值的值的话则在函数调用时一定要在相应位置传入undefined

    剩余参数编译后

    function bulidName(firstName) {
        if (firstName === void 0) { firstName = 'Bob'; }
        var restOfName = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            restOfName[_i - 1] = arguments[_i];
        }
        return firstName + '' + restOfName;
    }
    var result = bulidName('Jack', 'smith');
    var result1 = bulidName(undefined, 'Jons');
    var result3 = bulidName('Jack', 'smith', 'Niclos');
    console.log(result); //Jacksmith
    console.log(result1); //BobJons
    console.log(result3);

    带有剩余参数的函数定义

    function bulidName(firstName = 'Bob', ...restOfName: string[] ): string {
      return firstName + '' + restOfName
    }
    //带有剩余参数的函数定义
    let bulidNameFn: (fname: string, ...rest: string[]) => string = bulidName

    2019-05-28  11:38:16

    工欲善其事,必先利其器
  • 相关阅读:
    python百度ai的银行卡识别代码
    python百度ai的身份证识别代码
    Linux下安装jupyter
    HADOOP 与 jupyterlab 链接
    csv文件数据导出到mongo数据库
    Contos7 常用命令
    centos 安装Python3 及对应的pip
    PHP 连接数据库
    java 注解学习记录
    java简单实现搜索指定后缀文件
  • 原文地址:https://www.cnblogs.com/ccbest/p/10936259.html
Copyright © 2011-2022 走看看