Typescript参数:
1.参数类型:
使用规则:在参数名称后面使用冒号来指定参数类型。
常见的类型:string、number、any、boolean;
var myname:string = "zhangming";
var alias: any = "xixi";
var age: number = 13;
var man: boolean = true;
2.函数参数的可选参数
function test(a: string, b: string, c: string) {
console.log(a);
console.log(b);
console.log(c);
}
test("a", "b");//此处会报错,因为参数个数不够
test("a","b","c");
function test(a: string, b?: string, c: string="jojo") {
console.log(a);
console.log(b);
console.log(c);
}
function test1(a?: string, b: string, c: string="jojo") {
//仍会报错,可选参数不能在必选参数前面
}
test("xx");//可以顺利调用 注:第二段代码不会报错是因为b是一个可选参数(加了问号,所以可以不用传值,相当于未定义的一个数)
TypeScript 参数详解
本文介绍了 TypeScript 中参数的基本概念,包括参数类型的定义方式及其常见类型,如 string、number 等。此外,还详细讲解了函数参数中的可选参数及默认值设置的方法。
574

被折叠的 条评论
为什么被折叠?



