ts 默认参数 可选参数 剩余参数

ts 默认参数 可选参数 剩余参数

  • 默认参数
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
上一篇:Scrum培训感想


下一篇:[Go] 结构体成员的第三个位置标签的作用