understanding $ vs. jQuery in iife instead of $

2020-01-29 18:22发布

I am trying to understand if there is any difference between:

(function($){

...

})(jQuery);

vs.

(function($){

...

})($);

Note the jQuery was replaced with a $. Is this ok? Is it not used anywhere because it can't work? It works but maybe it is non-standard? Can someone please explain this if it is an error or if it is ok? Thanks

标签: jquery iife
4条回答
Anthone
2楼-- · 2020-01-29 18:27

Your second example is redundant. If you can pass jQuery using $ then $ is already jQuery, i.e. you could have just wrote:

(function(){
   $('#selectme');
})();

The first example, is only worthwhile if you want to use jQuery in noConflict mode, but still use the $ to reference jQuery in select pieces of code. Otherwise, you technically don't even need a closure (although closures are good practice for numerous other reasons).

查看更多
在下西门庆
3楼-- · 2020-01-29 18:42

Other JavaScript frameworks may also use $ as a shortcut. To guarantee that $ is jQuery inside your function, pass jQuery and not $ at the end. This type of defining a function or "code area" is to take sure that $ really is jQuery when mixing frameworks.

查看更多
等我变得足够好
4楼-- · 2020-01-29 18:46

There is.
$ is just a shortcut for jQuery. It does not define the lib, therefore it may be used by other frameworks too.
Take this case into consideration :

// remove the jQuery shortcut ($ === undefined)
var j = jQuery.noConflict();
// redefine the dollar sign
window.$ = function(){
    // some other plugin
}

// case A
(function($){
   alert(jQuery === $);//true
})(jQuery)

 // case B
(function($){
   alert(jQuery === $);//false
})($)
查看更多
爷、活的狠高调
5楼-- · 2020-01-29 18:48

Many JavaScript libraries use $ as a function or variable name, just as jQuery does. In jQuery's case, $ is just an alias for jQuery, so all functionality is available without using $. If we need to use another JavaScript library alongside jQuery, we can return control of $ back to the other library with a call to $.noConflict():

http://api.jquery.com/jQuery.noConflict/

In "no-confict" mode, the $ shortcut is not available and the longer jQuery is used. For example:

$(document).ready(function(){
     $(#somefunction) ...
});

Becomes:

jQuery(document).ready(function(){
    jQuery(#somefunction) ...
});

In order to use the default jQuery shortcut of $, you can use the following wrapper around your code:

jQuery(document).ready(function($) {
    // $() will work as an alias for jQuery() inside of this function
});

That wrapper will cause your code to be executed when the page finishes loading, and the $ will work for calling jQuery. If, for some reason, you want your code to execute immediately (instead of waiting for the DOM ready event), then you can use this wrapper method instead:

(function($) {
    // $() will work as an alias for jQuery() inside of this function
})(jQuery);

Good read: http://codex.wordpress.org/Function_Reference/wp_enqueue_script#jQuery_noConflict_wrappers

Further if you keen:

What does $ mean in jQuery?

This should help to quench your thirst :) might be hope this helps!

查看更多
登录 后发表回答