ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • ν•¨μˆ˜μ˜ νƒ€μž…λͺ…μ‹œ
    ⌨️ conceptualization/Typescript 2022. 9. 11. 18:37
    728x90

    πŸ“Œ typescriptμ—μ„œ ν•¨μˆ˜λ₯Ό μž‘μ„±ν•˜λŠ” 방법에 λŒ€ν•΄ μ•Œμ•„λ³΄μž

    1. ν•¨μˆ˜μ˜ λ°˜ν™˜(return) νƒ€μž…μ„ μ§€μ •ν•˜λŠ” 방법
    2. 선택적 λ§€κ°œλ³€μˆ˜(parameter)λ₯Ό μ „λ‹¬ν•˜λŠ” 방법
    3. λ§€κ°œλ³€μˆ˜μ˜ λ””ν΄νŠΈ 값을 μ£ΌλŠ” 방법

     

     

    1. ν•¨μˆ˜μ˜ λ°˜ν™˜(return) νƒ€μž…μ„ μ§€μ •ν•˜λŠ” 방법

    syntax

    function ν•¨μˆ˜μ΄λ¦„(λ§€κ°œλ³€μˆ˜1, λ§€κ°œλ³€μˆ˜2) : ν•¨μˆ˜μ˜ λ°˜ν™˜νƒ€μž… {
    
    }

     

     

     

    ❗️ void νƒ€μž…

    function test(): void {}

    πŸ‘‰πŸ» 아무것도 λ°˜ν™˜ ν•˜μ§€ μ•ŠλŠ” ν•¨μˆ˜μ˜ λ°˜ν™˜ κ°’μœΌλ‘œλ§Œ μ‚¬μš©λ  수 μžˆλŠ” νƒ€μž…

     

     

     

    string νƒ€μž…

    function test(): string {
      return "a";
    }

    πŸ‘‰πŸ» string κ°’μœΌλ‘œ return

     

     

     

    string[] νƒ€μž…

    function test(): string[] {
      return ["a", "b"];
    }

    πŸ‘‰πŸ» string λ°°μ—΄ κ°’μœΌλ‘œ return

    πŸ”‘ number와 booleanλ“± λ‚˜λ¨Έμ§€ νƒ€μž…μ—λ„ λ™μΌν•˜κ²Œ μ μš©λœλ‹€.

     

     

    2. 선택적 λ§€κ°œλ³€μˆ˜(parameter)λ₯Ό μ „λ‹¬ν•˜λŠ” 방법

    • λ‹€μŒκ³Ό 같은 ν•¨μˆ˜κ°€ μžˆλ‹€κ³  κ°€μ •ν•΄λ³΄μž
    function test(a: string, b: string): void {
      console.log(a, b);
    }
    
    test("hi", "yelim");

    πŸ“Œ λ§Œμ•½ μ—¬κΈ°μ„œ 인자(argment)값을 ν•˜λ‚˜λ§Œ μ „λ‹¬ν•œλ‹€κ³  ν•˜λ©΄ errorκ°€ λ°œμƒν•  것이닀.

     

     

     

     

    ❓ μ™œ κ·ΈλŸ°κ²ƒμΌκΉŒ ?

    νƒ€μž…μŠ€ν¬λ¦½νŠΈλŠ” ν•¨μˆ˜μ— μ •μ˜λœ λͺ¨λ“  λ§€κ°œλ³€μˆ˜κ°€ ν•¨μˆ˜μ— ν•„μš”ν•˜λ‹€κ³  κ°€μ •ν•˜κΈ° λ•Œλ¬Έμ΄λ‹€.
    -> ν•¨μˆ˜ ν˜ΈμΆœμ‹œ νƒ€μž…μŠ€ν¬λ¦½νŠΈ μ»΄νŒŒμΌλŸ¬λŠ” parameter 와 argumentλ₯Ό 비ꡐ κ²€μ‚¬ν•˜κ²Œ λœλ‹€.

     

     

    🧠 μ–΄λ–»κ²Œ ν•΄κ²°ν• μˆ˜ μžˆμ„κΉŒ?

    이전에 νƒ€μž…λͺ…μ‹œμ™€ 같이 μžˆμ–΄λ„ 되고 없어도 λ˜λŠ” κ°’ !
    선택적 λ§€κ°œλ³€μˆ˜(optional parameter)λ₯Ό μ‚¬μš©ν•˜μ—¬ ν•΄κ²°ν•΄λ³΄μž
    function test(a: string, b?: string): void {
      console.log(a, b);
    }
    
    test("hi");

     

     

    ❗️ μ—¬κΈ°μ„œ μ£Όμ˜μ‚¬ν•­

    λ§Œμ•½ μ „λ‹¬λ˜λŠ” λ§€κ°œλ³€μˆ˜κ°€ μ—¬λŸ¬κ°œμ΄κ³ ,
    λͺ‡κ°€μ§€λ§Œ 선택적 λ§€κ°œλ³€μˆ˜μΈ 경우 ??

     

    πŸ‘‰πŸ» 선택적 λ§€κ°œλ³€μˆ˜λ“€μ€ λ°˜λ“œμ‹œ ! ν•„μˆ˜ λ§€κ°œλ³€μˆ˜ 뒀에 μœ„μΉ˜ν•΄μ•Όν•œλ‹€ !

    function test(a: string, b?: string, c?: string, d?: string, e?: string): void {
      console.log(a, b, c, d, e);
    }
    
    test("hi");

     

    • 컴파일 μ‹œμΌœμ„œ console둜 μ‚΄νŽ΄λ³΄κ²Œ 되면 λ‹€μŒκ³Ό 같이 ν• λ‹Ήλ˜μ§€ μ•Šμ€ argument값은 parameter둜 μ „λ‹¬λ˜μ§€ μ•Šμ•„ undefined둜 ν‘œμ‹œλœλ‹€.
    hi undefined

     

     

     

    πŸ‘‰πŸ» μ΄λŸ΄λ•Œ ν• λ‹Ήλœ 기본값을 μ‚¬μš©ν• μˆ˜ μžˆλ‹€ ? λ°”λ‘œ

    3. κΈ°λ³Έ 맀개 λ³€μˆ˜(Default Parameter)

    function test(a: string, b = 'yelim'): void { 
      console.log(a, b);
    }
    
    test("hi");

    πŸ“Œ κ·Έλƒ₯ parmeter 뒀에 값을 μ„€μ •ν•΄μ£Όλ©΄ λœλ‹€.
    πŸ“Œ λ§€κ°œλ³€μˆ˜λ’€μ— 값을 μ„€μ •ν•΄ μ£Όλ©΄λœλ‹€.(μ„ νƒμ ν‘œκΈ°(?), νƒ€μž… μƒλž΅κ°€λŠ₯ -> νƒ€μž…μΆ”λ‘ μœΌλ‘œ 컀버가λŠ₯)

    πŸ‘‰πŸ» λ‹€μŒκ³Ό 같은 μ½”λ“œμ—μ„œ 무엇이 좜λ ₯될지 μƒκ°ν•΄λ³΄μž !

    function test(a = 'hello', b = 'yelim'): void { 
      console.log(a, b);
    }
    
    test(); 
    test('hi');
    test('hi','there');
    
    
    
    
    // 1. πŸ“Œ hello yelim
    // 2. πŸ“Œ hi yelim
    // 3. πŸ“Œ hi there

     

     

     

    (μ°Έκ³ μ˜μƒ) 땅콩코딩 : https://www.youtube.com/watch?v=SVtqhpboxGw&t=2s

    λŒ“κΈ€