TypeScript--函数
javascript中的函数
function test1(){ alert("test1") } var test2 = function(){ alert('test2'); }
TypeScript中的函数
没有参数没有返回值
function run0():void{ console.log('aa') }
没有参数有返回值
function run():string{ return '123' }
有参有返回值
function run3(name:string,age:number):string{ return `${name}---${age}` } alert(run3('aa',20))
有参没有返回值
function run4(name:string,):void{ console.log(name) } alert(run4('aaa'))
默认参数,可选参数
可选参数要放在最后面
function run5(age:number,name?:string):string{ if(name){ return `${name}--${age}` }else{ return `${age}` } } run5(20) run5(20,'aa') run5('aa',20)
剩余参数
function sum(a:number,b:number,c:number):number{ return a+b+c; } alert(sum(1,2,3)); function sum2(...result:number[]):number{ var sum =0; for(var i =0;i<result.length;i++){ sum+=result[i] } return sum; } alert(sum2(1,2,3,4));
函数重载
function getInfo(name: string): string; function getInfo(age: number): string; function getInfo(str: any): any { if (typeof str === 'string') { return '我叫' + str; } else { return '我的年龄是' + str; } } alert(getInfo('aa')) alert(getInfo(11))
箭头函数
setTimeout(() => { alert('aa') },1000) setTimeout(function(){ alert('bb') },2000)
相关推荐
ChaITSimpleLove 2020-10-06
ChaITSimpleLove 2020-07-26
iconhot 2020-07-05
Lzs 2020-10-23
聚合室 2020-11-16
零 2020-09-18
Justhavefun 2020-10-22
jacktangj 2020-10-14
Andrea0 2020-09-18
周游列国之仕子 2020-09-15
afanti 2020-09-16
88234852 2020-09-15
YClimb 2020-09-15
风雨断肠人 2020-09-04
卖口粥湛蓝的天空 2020-09-15
stulen 2020-09-15
pythonxuexi 2020-09-06