TypeScript快速笔记(二)

1) TypeScript中的判断语句,可以使用非0值代表true。如:

 1 function add1(a: number, b?:number): number{ // 注意b是可选参数
 2     console.log("parm1(a): " + a, "parm2(b): " + b);
 3     if(b){// 可选参数b,如果没有传参,值为undefined
 4         return a + b;
 5     }
 6     else{
 7         return a;
 8     }
 9 }
10 console.log(add1(10, 20)); // a: 10, b: 20
11 console.log(add1(25)); // a: 25, b: undefined
12 
13 var num: number = 5;
14 if (num){
15     console.log('非0就是true') // 执行
16 }
17 num = 0;
18 if (num){
19     console.log('Test'); // 不会执行
20 }
21 
22 if (null){
23     console.log('null') // 不会执行
24 }
25 if (undefined){
26     console.log('undefined') // 不会执行
27 }
非0判断

2)函数的定义有多种:无参函数,有参函数,可选函数,默认参数(与C/C++类似),剩余参数(类似于C/C++的可变参数),匿名函数,构造函数,递归函数,Lambda函数

 

普通参数的定义方式:

function functionName(){}
function functionName(parm1: [type], parm2: [type]): returnType{}

 可选参数的定义方式:

function functionName(parm1:[type], parm2?: [type]): returnType{} // 在可选参数后面加‘?’

实例:

 1 console.log("普通参数测试:");
 2 function add(a: number, b: number): number{
 3     return a + b;
 4 }
 5 
 6 console.log(add(5, 7));
 7 
 8 // 可选参数,当没有传进可选参数时,参数值为undefined
 9 console.log("可选参数测试:");
10 function add1(a: number, b?:number): number{
11     console.log("parm1(a): " + a, "parm2(b): " + b);
12     if(b){// 可选参数b,如果没有传参,值为undefined
13         return a + b;
14     }
15     else{
16         // NaN代表一个非数字值
17         console.log(isNaN(a + b)); // true
18         console.log(isNaN(undefined)); // true
19         console.log(isNaN(null)); // false, null不是NaN值
20 
21         return a;
22     }
23 }
24 
25 console.log(add1(10, 20));
26 console.log(add1(25));
27 
28 
29 // 剩余参数
30 console.log("剩余参数测试:");
31 function sum(...num: number[]){
32     let sum: number = 0;
33     let resultString: string = '';
34     for(let i = 0; i < num.length; ++i){
35         sum += num[i];
36         resultString += num[i];
37         if (i + 1 < num.length)
38             resultString += ' + ';
39         else
40             resultString += ' = ';
41     }
42     resultString += sum;
43     console.log(resultString);
44     return sum;
45 }
46 
47 sum(1,2,3,4,5);
48 
49 //匿名函数
50 // 除了没有函数名外,其他的与标准函数一样。将匿名函数赋值给一个变量,这种表达式就成为函数表达式。
51 var res = function(a: number, b: number): number{
52     return a + b;
53 }
54 
55 console.log(res(1, 5));
56 
57 // 匿名函数自调用,在函数后面加个圆括号即可
58 (function(){
59     console.log("Hello World!");
60 })();
函数

结果:

普通参数测试:
12
可选参数测试:
parm1(a): 10 parm2(b): 20
30
parm1(a): 25 parm2(b): undefined
true
true
false
25
剩余参数测试:
1 + 2 + 3 + 4 + 5 = 15
6
Hello World!
函数实例输出结果

补充:关于重载函数,除了使用any,也可以使用联合类型,即:function disp(x:string|number,y?:string|number):void{}

 3)TypeScript中的重载不是真正意义的重载,不能想C++/C#等方式直接重载,否则编译时会出现报错:TS2396: Duplicate function implementation.

  TypeScript重载方式:

    *如果参数类型不同,则参数类型应设置为 any

    *参数数量不同你可以将不同的参数设置为可选。

 1 // 函数重载
 2 function disp(s1:string):void;  // 先声明重载函数但是不定义(函数签名)
 3 function disp(n1:number,s1:string):void;
 4 function disp(s1:string,n1:number):void;
 5  
 6 // 根据函数重载方式的实际情况,函数体的类型判断也可以做出优化
 7 function disp(x:any,y?:any):void { // 通过any类型和可选参数实现重载
 8     if(typeof(x) == "string" && typeof(y) == "undefined"){
 9         console.log('(string): ' + x);
10     }
11     else if(typeof(x) == 'number' && typeof(y) == 'string'){
12         console.log('(number, string):' + x + ',' + y);
13     }
14     else if(typeof(x) == 'string' && typeof(y) == 'number'){
15         console.log('(string, number):' + x + ',' + y);
16     }
17 } 
18 disp("abc"); // 输出:(string): abc
19 disp(1,"xyz"); // 输出:(number, string):1,xyz
20 disp("hello", 100); // 输出:(string, number):hello,100
21 //disp(1, 1); // 类型不匹配会报错:TS2345: Argument of type '1' is not assignable to parameter of type 'string'.
函数重载

4)Number对象是原始数值number的包装对象,封装了更多的方法。

 1 // Number对象测试
 2 // Number对象是原始数值的包装对象
 3 var num1: number = 100;
 4 var num2 = new Number(num1); 
 5 var num3 = new Number('123'); // 可以接受字符串转换
 6 var num4 = new Number(null); // 0
 7 var num5 = new Number('0xA'); // 十六进制,等于十进制的10
 8 var num6 = new Number('123a'); //  如果一个参数值不能转换为一个数字将返回非数字值 NaN (Not a Number)
 9 
10 console.log('Test typeof:');
11 console.log(typeof num1); // 输出:number
12 console.log(typeof num2); // Number是一个对象,输出:object
13 console.log(typeof num3); // 输出:object
14 console.log(typeof num4); // 输出:object
15 console.log(typeof num5); // 输出:object
16 console.log(typeof num6); // 输出:object
17 
18 console.log('Value test:');
19 console.log(num1.toString()); // 100
20 console.log(num2.toString()); // 100
21 console.log(num3.toString()); // 123
22 console.log(num4.toString()); // 0
23 console.log(num5.toString()); // 10
24 console.log(num6.toString()); // NaN
25 
26 //console.log(num2 + num3); 编译错误,TS2365: Operator '+' cannot be applied to types 'Number' and 'Number'.
number类型和Number对象测试

 

posted @ 2019-09-14 22:58  SunboyL  阅读(551)  评论(0编辑  收藏  举报