1.错误

  1. function go(a:number,b:number){
  2. return a+b
  3. }
  4. go(1,"12");

2.正确

  1. function go(a:number,b:number):number{ //:number 函数返回值是number
  2. return a+b
  3. }
  4. go(1,12);

函数

  1. /* 没有返回值 */
  2. function go():void{ ----void:没有指定数据类型
  3. console.log("go");
  4. }
  5. /* 有返回值 */
  6. function show(val:string):string{
  7. return val
  8. }

1.默认参数

  1. var str = "hello world";
  2. function http(method='get'){ //method
  3. console.log(method);
  4. }
  5. http();
  6. http('post') //method='post'
  1. function http(method:string='get'):void{
  2. console.log(method);
  3. }
  4. http();
  5. http('post')

2.可选参数

  1. function getInfo(name:string,age?:number):void{
  2. console.log(name+age);
  3. }
  4. getInfo('Nihao')