函数
函数的定义
- 可以指定参数的类型和返回值的类型
typescript
function hello(name:string):void {
console.log('hello',name);
}
hello('zhangsan'); // hello zhangsan
函数表达式
- 定义函数类型
typescript
type GetName = (firstName: string, lastName: string) => string
let getName: GetName = function (firstName: string, lastName: string): string {
return firstName + lastName
}
没有返回值
typescript
let hello2 = function (name:string):void {
console.log('hello2',name);
return undefined;
}
hello2('zs');
可选参数
在TS中函数的形参和实参必须一样,不一样就要配置可选参数,而且必须是最后一个参数
typescript
function print(name:string,age?:number):void {
console.log(name,age);
}
print('zhangsan');
默认参数
typescript
function ajax(url:string,method:string='GET') {
console.log(url,method);
}
ajax('/users');
剩余参数
typescript
function sum(...numbers:number[]) {
return numbers.reduce((val,item)=>val+=item,0);
}
console.log(sum(1,2,3));
函数重载
- 在Java中的重载,指的是两个或者两个以上的同名函数,参数不一样
- 在TypeScript中,表现为给同一个函数提供多个函数类型定义
typescript
let obj: any={};
function attr(val: string): void;
function attr(val: number): void; // 紧挨着主体函数
function attr(val:any):void {
if (typeof val === 'string') {
obj.name=val;
} else {
obj.age=val;
}
}
attr('zs');
attr(9);
attr(true);
console.log(obj);