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

【すっきりしたい】即時関数の記載が気持ち悪い

More than 1 year has passed since last update.

他の言語でも同じことだと思う。
けど、JavaScriptで遭遇しがちなのでJavaScript用の記事に。

目的

Aさん「即時関数の記述方法が気持ち悪い・・」
Bさん「なんであんな書き方なの・・」

そんなあなたをすっきりさせたい。

あと、自分もちょいちょい忘れるので、メモしたい。

気持ち悪い
(function () {
  //気持ち悪い
})();

参考記事

http://unitopi.com/js-function/

これがとてもわかりやすいのだが、
忘れた時に探しに行く苦労や、記事自体がなくなってしまったら
私が泣いてしまうので、自分のメモ代わりに転載。

説明

[1]関数を使うことを考える

hoge();

[2]hoge(の名前のところ)を即時関数にしてみる

(
function hoge(){}
)();

[3]hogeという名前すらいらなくなる(無名関数化)

(
function () {}
)();

[4]整えて、完成

もう気持ち悪くない
(function () {
  //もう気持ち悪くない
})();

ね!

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