且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

这个“(function(){});",括号内的函数,在javascript中是什么意思?

更新时间:2022-12-28 09:36:26

您立即使用特定参数调用匿名函数.

You're immediately calling an anonymus function with a specific parameter.

一个例子:

(function(name){
  alert(name);
})('peter')

这会提醒peter".

在 jQuery 的情况下,您可以将 jQuery 作为参数传递并在您的函数中使用 $.所以你仍然可以在 noConflict 模式下使用 jQuery,但使用方便的 $:

In the case of jQuery you might pass jQuery as a parameter and use $ in your function. So you can still use jQuery in noConflict-mode but use the handy $:

jQuery.noConflict()
(function($){
  var obj = $('<div/>', { id: 'someId' });
})(jQuery)