jQuery newbie: what does jQuery(function($) { … }) means?

后端 未结 5 1538
旧时难觅i
旧时难觅i 2020-12-13 14:11

I know in jQuery, $(callback) is the same as jQuery(callback) which has the same effect as $(document).ready().

How about

5条回答
  •  清歌不尽
    2020-12-13 14:52

    So I was corrected on this and if you read the first comment it gives some context.

    jQuery(function() {
        // Document Ready
    });
    
    
    (function($) {
        // Now with more closure!
    })(jQuery);
    

    I'm not 100% sure but I think this just passes the jQuery object into the closure. I'll do some digging on the google to see if I am right or wrong and will update accordingly.

    EDIT:

    I'm pretty much right, but here it is straight from their website:

    http://docs.jquery.com/Plugins/Authoring

    "Where's my awesome dollar sign that I know and love? It's still there, however to make sure that your plugin doesn't collide with other libraries that might use the dollar sign, it's a best practice to pass jQuery to a self executing function (closure) that maps it to the dollar sign so it can't be overwritten by another library in the scope of its execution."

提交回复
热议问题