在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
// 类型别名 type ISum = (x: number, y: number) => number // 应用如下: let sum: ISum = (a, b) => a + b console.log(sum(3, 2));
//函数重载 function fn(name: string): string; function fn(age: number): number; function fn(nameorage: any): any { if (typeof nameorage == "string") { return "姓名:" + nameorage; } else if (typeof nameorage == "number") { return "年龄:" + nameorage } } // 姓名:张培跃 console.log(fn("张2")); // 年龄:18 console.log(fn(666));
//======函数重载====反转数字或字符串 function reverse(num: number): number; function reverse(str: string): string; function reverse(val: any): any { if (typeof val === 'number') { return Number(val.toString().split('').reverse().join('')) } else if (typeof val === 'string') { return val.split('').reverse().join('') } return false } console.log(reverse(123456)) // 654321 console.log(reverse('sina')) // 'anis'
//函数的 剩余参数 function sum2(num1: number, ...rest: number[]): number { interface ITotal { (pre: number, cur: number): number; } const handle: ITotal = (pre, cur) => pre + cur return rest.reduce(handle, num1) } console.log(sum2(1, 2, 3, 4, 5, 6, 7)); // 28
//====缺省的第1个参数,this 指针 function fun3(this:any, para1:number,para2:number) { console.log(para1); console.log(para2); } fun3(1,2);//1,2
|
请发表评论