LoginSignup
4
5

More than 5 years have passed since last update.

最近まで引数のある即時関数が謎だった件

Posted at
sample
(function(w,$){
    console.log(w);
    console.log($("a").text());
})(window,jQuery);

こういうのたまに見かけますが、
最近まで謎でした。。
理解すべくシンプルにしてみました

sample
(function (name) {
    console.log(name);
})("まさみ");

/*ログの結果は↓*/
// まさみ
sample
function loveCall (name) {
    console.log(name);
}
loveCall("まさみ");

のloveCall関数の定義とコールを
省略してるわけですね。

これでjQueryの他のライブラリとの衝突防止の
おまじないの意味なども理解できるはず!

sample
(function($){
    console.log($('#sample').text());
})(jQuery);

以上、明日もがんばりましょう!

4
5
0

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