这样做的目的是什么? (function($){// function here here})(jQuery); [英] What is the purpose of this? (function ($) { //function code here })(jQuery);

查看:110
本文介绍了这样做的目的是什么? (function($){// function here here})(jQuery);的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 (function($) {
//大部分代码在这里...
})(jQuery);

($)(jQuery)?我没有被教会编码这样,并没有看到他们。他们的目的是什么?



同样,没有 document.ready ,但我认为这是因为代码是在它被(); 读完后执行的吗?

解决方案

  var $ =有些值我们不关心; 

// v ===== normal plain old function
(function($){
// ^ =======接收jQuery对象作为$参数

//这里的大部分代码,其中$ === jQuery ...
$ b $('。myclass')。do()。crazy()。things ();


})(jQuery);
// ^ =======立即调用,并传递了jQuery对象


//在这里,$是不受干扰的
alert($) ; //我们不在乎的一些价值


I am debugging someone else's JavaScript code and a majority of the code is wrapped like this:

(function ($) {
    //majority of code here...
})(jQuery);

What is going on with the ($) and the (jQuery)? I wasn't taught to code like that and haven't seen them. What is their purpose?

As well, there is no document.ready, but I assume that is because the code is executed right after it's read by the (); at the end?

解决方案

var $ = "some value we don't care about";

 // v=====normal plain old function
(function ($) {
 //        ^=======receives jQuery object as the $ parameter

    //majority of code here, where $ === jQuery...

    $('.myclass').do().crazy().things();


})(jQuery);
 //  ^=======immediately invoked, and passed the jQuery object


 // out here, $ is undisturbed
alert( $ ); // "some value we don't care about"

这篇关于这样做的目的是什么? (function($){// function here here})(jQuery);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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