函数的完整写法

 //1.函数声明写法
 //小括号后面的:number代表的是返回值
function add(x:number,y:number) :number{
    return x+y
}

const result = add(1,2)
console.log(result);

//2.函数表达式

const add2 = function(x:string,y:string) :string {
    return x+y
}

const result1 = add2('111','222')
console.log(result1);


//3.函数的完整写法
//(x:string,y:string) => number 表示当前这个函数的类型
//function(x:string,y:string) :number{  return parseInt(x) + parseInt(y) } 就相当于符合上面条件的返回值
   
const add3:(x:string,y:string) => number = function(x:string,y:string) :number{
    return parseInt(x) + parseInt(y)



const result3 = add3('1','2');
console.log(result3);

结果:
typeScript中函数声明以及函数表达式

相关文章:

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