即時関数とは
関数定義と同時に一度だけ実行される関数。
即時じゃない関数
普通の関数
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!