Typescript默認參數和可選參數


TypeScript默認參數

  function test (a : string, b: string, c : string = "abc" ) {

    console.log(a)

    console.log(b)

    console.log(c)

  }

  test("aaa","bbb") // 執行結果 aaa, bbb, abc  c中的值為方法的默認值abc   注:使用默認值參數的時候必須放在參數的最后一位

 

TypeScript可選參數

  function test (a : string, b?: string, c : string = "abc" ) {

    console.log(a)

    console.log(b)

    console.log(c)

  }

  test("aaa")  // 執行結果 aaa, undefined,  abc   注:可選參數必須聲明在必須參數之后


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM