• 默认参数
function buildName(firstName: string, **lastName = "Smith"**) {
    return firstName + " " + lastName;
}

let result1 = buildName("Bob");                  // "Bob Smith"
  • 可选参数
function buildName(firstName: string, lastName?: string) {
    if (lastName)
        return firstName + " " + lastName;
    else
        return firstName;
}

let result1 = buildName("Bob");  // Bob
let result2 = buildName("Bob", "Adams");  // Bob Adams
  • 剩余参数
function buildName(firstName: string, ...restOfName: string[]) {
  return firstName + " " + restOfName.join(" ");
}

let employeeName = buildName("Joseph", "Samuel", "Lucas", "MacKinzie"); //Joseph Samuel Lucas MacKinzie

相关文章:

  • 2021-12-20
  • 2022-12-23
  • 2022-12-23
  • 2021-11-28
  • 2021-11-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-21
  • 2021-09-06
  • 2022-12-23
  • 2022-01-01
  • 2022-12-23
  • 2021-05-12
  • 2021-12-29
相关资源
相似解决方案