ts中的可选参数

可选参数

/* 
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('诸葛','孔明'));

原文地址:https://www.cnblogs.com/malong1992/p/14626516.html