zoukankan      html  css  js  c++  java
  • 【typescript 03】函数的定义

    old

    1. 声明函数

    • function funcA() {
          console.log('AAA');
      }

    2. 匿名函数

    • let funcB = () => {
          console.log('BBB');
      }

    ts:

    多出了的额外工作:输入(需要制定类型)输出(需要制定类型)

    1. 声明函数

    • function tsFuncGirl(name: string, age: number): void {
          const person = {
              name,
              age,
              sex: 'girl'
          }
          console.log(JSON.stringify(person));
      }
      
      tsFuncBoy('kjf', 24);

    2. 匿名函数

    • let tsFuncBoy = (name: string, age: number): string => {
          const person = {
              name,
              age,
              sex: 'boy'
          }
          return JSON.stringify(person);
      }
      
      console.log(tsFuncBoy('kjf', 24));

    可选参数(可传可不传的参数,必须是最后的若干个参数,即不能 function func(a:number, b?: number, c: number) 

    • let tsFuncBoy = (name: string, age?: number): string => {
          const person = {
              name,
              age: age || '保密',
              sex: 'boy'
          }
          return JSON.stringify(person);
      }
      
      console.log(tsFuncBoy('kjf'));

    默认参数(es5 无法设置默认参数,ts 和 es6 都可以设置默认参数)

    • let tsFuncPerson = (name: string, age?: number, sex: string = 'boy'): string => {
          const person = {
              name,
              age: age || '保密',
              sex
          }
          return JSON.stringify(person);
      }
      
      console.log(tsFuncPerson('god', 18, 'girl'));

    剩余参数

    3

    3

    3

    3

  • 相关阅读:
    3 saltstack高可用
    2 salt-masterless架构
    1 salt执行模块开发
    git push 后 链接总是灰色点击没有反应
    4. Beego 框架之cookie与session
    19 Go的全能ORM简单入门
    K8S
    Docker
    Python
    TS
  • 原文地址:https://www.cnblogs.com/tianxiaxuange/p/13423941.html
Copyright © 2011-2022 走看看