Help us understand the problem. What is going on with this article?

[JavaScript] "!function() {}()"以外の即時関数を紹介するぜ

ヒャッハー!!IIFEなメンバーを紹介するぜえ!!:metal:

IIFE (Immediately Invoked Function Expression; 即時実行関数式) は定義されるとすぐに実行される JavaScript の 関数 です。 1

初級編

即時関数として知られ使われるやつ。

(function() {console.log('a')})()
(function() {console.log('a')}())
!function() {console.log('a')}()
(() => {console.log('a')})()

中級編

調べれば出てきたりするけど普通使わない。

// ↓それでも使われる可能性あり
+function() {console.log('a')}()
~function() {console.log('a')}()
-function() {console.log('a')}()
void function() {console.log('a')}()
// ↓みたこと無い
typeof function() {console.log('a')}()
new function() {console.log('a')}()
await function() {console.log('a')}()
delete function() {console.log('a')}()

上級編

レビュアーや保守の人などをあっと言わすことができる。(悪い意味で)

new Function('console.log("a")')()
0 ** function() {console.log('a')}()
0 / function() {}(console.log('a'))
0 * function() {console.log('a')}()
0 % function() {console.log('a')}()
0 << function() {console.log('a')}()
0 >> function() {console.log('a')}()
0 >>> function() {console.log('a')}()
0 < function() {console.log('a')}()
0 > function() {console.log('a')}()
0 <= function() {console.log('a')}()
0 >= function() {console.log('a')}()
new Function('console.log("a")')() instanceof Function
0 in [function() {console.log('a')}()]
0 & function() {console.log('a')}()
0 ^ function() {console.log('a')}()
0 | function() {console.log('a')}()
1 && function() {console.log('a')}()
0 || function() {console.log('a')}()
null ?? function() {console.log('a')}()
Function?.(function() {console.log('a')}())
0 == function() {console.log('a')}()
0 === function() {console.log('a')}()
0 != function() {console.log('a')}()
0 !== function() {console.log('a')}()
0 , function() {console.log('a')}()
NaN = function() {console.log('a')}()
NaN *= function() {console.log('a')}()
NaN /= function() {console.log('a')}()
NaN %= function() {console.log('a')}()
NaN += function() {console.log('a')}()
NaN -= function() {console.log('a')}()
NaN <<= function() {console.log('a')}()
NaN >>= function() {console.log('a')}()
NaN >>>= function() {console.log('a')}()
NaN &= function() {console.log('a')}()
NaN ^= function() {console.log('a')}()
NaN |= function() {console.log('a')}()
NaN **= function() {console.log('a')}()

番外編

こんな即時関数(?)も考え始めたけど、ふと我に返り途中でやめた。

(function() {console.log('a')})``
if (function() {console.log('a')}());
for (function() {console.log('a')}();false;);

感想

演算子試してみたみたいになっちゃいました。
これで謎記号がくっついたfunctionを見ても「なんだ即時関数か」と悩む人がいなくなれば嬉しいです。

kozzzz
疑問に思って調べたことなどを共有します。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした