1
1

More than 3 years have passed since last update.

即時関数の書き方 初心者向け

Last updated at Posted at 2020-08-20

即時関数とは

関数定義と同時に一度だけ実行される関数。

即時じゃない関数

普通の関数

function a() {
    console.log('called');
}

a();
//log >>> called

関数宣言での即時関数

関数宣言 =「say function!」

(function('仮引数') {
    console.log('called');
})('実引数');
//log >>> called

関数式での即時関数

returnで呼び出し元に実行結果が返却される。

下記の場合、変数aに変数bが入る。

const a = function('仮引数') {
    let b = 'hi!';
    return b;
}('実引数');

//関数式の場合functionの前後に()つけなくても動く、つけても動く

console.log(a);
//log >>> hi!

1
1
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
1
1