69
48

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.

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

Last updated at Posted at 2020-04-20

ヒャッハー!!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を見ても「なんだ即時関数か」と悩む人がいなくなれば嬉しいです。

  1. IIFE (即時実行関数式) - MDN Web Docs 用語集: ウェブ関連用語の定義 | MDN

69
48
4

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
69
48

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?