0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【備忘録】引数【TypeScript】

Last updated at Posted at 2020-12-24

引数

TypeScript,JavaScript 初学者が関数の引数について勉強したのでメモを残す。

デフォルト引数

  • 関数を呼び出す際に、引数を省略して実行するとデフォルト引数での処理が走る。

    const plusOne(n=1) => n+1;
    
    plusOne(2)  // 3
    plusOne()   // 2
    
  • 基本的にはデフォルト引数は、引数の後ろから書く。

    const plusOne(n=1,m) => n+m+1;  // NG
    const plusOne(m,n=1) => n+m+1;  // OK
    
  • falsy な値ついて

    • 引数にundefinedを渡すとデフォルト引数は既定値のまま
    • 引数にnullまたは空文字を渡すとnullもしくは空文字に設定される

残余引数(レストパレメータ)

  • ...という接頭辞をつけることで残りの引数を JavaScript の標準の配列として受け取ることができる。
const numbers = (num1, num2, ...rest) => {
  console.log(num1);
  console.log(num2);
  console.log(rest);
};

numbers(1, 2, 3, 4, 5, 6, 7);
//  1
//  2
//  [ 3, 4, 5, 6, 7 ]

参考

何か指摘等ございましたら、コメントでお願いいたします。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?