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判断

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-28
  • 2021-11-02
  • 2021-06-13
  • 2021-10-07
  • 2021-10-03
  • 2022-12-23
猜你喜欢
  • 2021-08-30
  • 2022-03-07
  • 2022-01-23
  • 2022-12-23
  • 2021-09-28
  • 2021-05-25
  • 2021-09-10
相关资源
相似解决方案