欢迎来到码农权的博客 MaNongGeGe.|

函数相关约束

函数的相关约束

函数重载:在函数实现之前,对函数调用的多种情况进行声明

可选参数:可以在某些参数名后加上问号,表示该参数可以不用传递。可选参数必须在参数列表的末尾。

 # 举个栗子-应用场景
 /**
   * 相乘的结果
   * @param a 
   * @param b 
   */
  function combine(a: number, b: number): number;
  /**
   * 相加的结果
   * @param a 
   * @param b 
   */
  function combine(a: string, b: string): string;
  function combine(a: number | string, b: number | string): number | string {
    if(typeof a === "number" && typeof b === "number") {
      return a * b;
    }else if(typeof a === "string" && typeof b === "string") {
      return a + b;
    }
    throw new Error("a和b必须是相同的类型")
  }
  combine(1, 2)

  function sum(a: number, b: number, c?: number) {
    if(c) {
      return a + b + c;
    }
  }
  function sum(a: number, b: number, c: number = 0) {
    return a + b + c;
  }

本文作者:HuangBingQuan

本文链接:https://www.cnblogs.com/bingquan1/p/17496835.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   HuangBingQuan  阅读(27)  评论(0编辑  收藏  举报
历史上的今天:
2022-06-21 React事件处理方法中this指向为undefined的四种解决方案
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 不将就 码农权
  2. 2 Bleeding Love 码农权
  3. 3 想你念你 码农权
Bleeding Love - 码农权
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

Not available