可选参数

/* 
1.如果什么都不传,则返回默认的
2.如果只传入姓氏,则返回姓氏
3.如果传入的姓氏和名字就返回姓名

*/

const fullName = function(firstName:string='东方',lastName?:string){
    if(lastName){
        return firstName + '-' +lastName
    }
    else {
        return firstName
    }
}

//什么都不传
console.log(fullName());

//只传姓氏
console.log(fullName('诸葛'));

console.log(fullName('诸葛','孔明'));

相关文章:

  • 2021-06-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-18
  • 2022-12-23
  • 2021-10-22
猜你喜欢
  • 2021-05-18
  • 2022-12-23
  • 2021-12-03
  • 2022-02-07
  • 2022-01-09
相关资源
相似解决方案