jQuery美元符号($)作为函数参数? [英] jQuery dollar sign ($) as function argument?

查看:314
本文介绍了jQuery美元符号($)作为函数参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解JavaScript闭包,我已经看到这在原生JS中完成了:

I understand JavaScript closures, and I've seen this done in native JS:

(function () {
  // all JS code here
})();

但是,添加jQuery spice有什么作用?

But, what does adding the jQuery spice do?

(function ($) {
  // all JS code here
})(jQuery);


推荐答案

它是一种将jQuery映射到$方式,所以不是页面中的所有代码都会看到它。

Its a way of mapping jQuery to the $ in a way so that not all code in a page will see it.

也许你有一个现有的脚本使用jQuery,你喜欢重用,但你也使用原型, $在同一页。

Maybe you have an existing script that uses jQuery that you like to reuse but you also use prototype that also uses $ in the same page.

通过使用代码在该结构中包装任何jQuery,你重新定义$到jQuery包含的部分,而不会与页面中的其他代码冲突。

By wrapping any jQuery using code in that construct you redefine $ to jQuery for the contained part without coming into conflict with other code in the page.

这篇关于jQuery美元符号($)作为函数参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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