Typescript常见三种函数类型

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_24147051/article/details/84987898

Typescript有常见三种函数类型:

  1. 分别是普通的函数;
  2. 有可选参数的函数;
  3. 有剩余参数的函数;

普通函数

function findMan(age:number):string{
  return 'find the '+ age + 'years'
}

有可选参数的函数

function findPeople(age:number,name?:string):string{
  if(name){
    return 'find the '+ age + 'years and name is'+ name;
  }else{
    return 'find the '+ age +'and no name!';
  }
}

有剩余参数的函数

function findRest(age:number,...xuqiu:string[]):string{
  let yy = "";
  for(let i=0; i< xuqiu.length;i++){
     yy = yy + xuqiu[i];
     if(i < xuqiu.length -1){
          yy = yy +'、'
     }
  }
  return '需求有:'+yy;
}

在这里查看打印结果:

var age:number = 18;
var result:string = findMan(age);
var resule2:string = findPeople(22,"HaoRen");
var resule3:string = findRest(22,'美女','人妖','SM','SB');

console.log(result);
console.log(resule2);
console.log(resule3);

猜你喜欢

转载自blog.csdn.net/qq_24147051/article/details/84987898