12
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

updated at

JavaScriptの無名関数

すぐ忘れてしまうのでめも

即時に実行する無名関数の書き方

関数内で宣言した変数は他のスクリプトに影響を与えない。

Twitterのボタン、Facebookのfb-rootなどもこの書式になっている

function_nameless.js
// 要素#hoge内にp要素を追加して「ほげほげ」とテキストを入れる
(function(d, t) {
    d.prepend('<p></p>');
    $('p', d).text(t);
})($('#hoge'), 'ほげほげ');
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
12
Help us understand the problem. What are the problem?