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
函数重载
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)