这个“(function(){});",括号内的函数,在javascript中是什么意思? [英] What does this "(function(){});", a function inside brackets, mean in javascript?

查看:40
本文介绍了这个“(function(){});",括号内的函数,在javascript中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
这是什么意思?(function (x,y)){…}){a,b);在 JavaScript 中
JavaScript 对象/函数周围的括号是什么/类声明是什么意思?

大家好

我不知道下面的内容是什么:

I don't know what the following does:

(function(){
  // Do something here
  ...
})(someWord) //Why is this here?;

我的问题是:

  1. 将函数放在方括号内是什么意思.即(function(){});?
  2. 这组括号在函数末尾有什么作用?

我通常在 jquery 代码和其他一些 javascript 库中看到这些.

I usually see these in jquery codes, and some other javascript libraries.

推荐答案

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

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)

这篇关于这个“(function(){});",括号内的函数,在javascript中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆