JQuery noConflict: Just a quick thought

The documentation states that you can use $ .noConflict () as follows:

jQuery.noConflict(); (function($) { $(function() { // more code using $ as alias to jQuery }); })(jQuery); // other code using $ as an alias to the other library 

It also states that calling it returns an instance of the jQuery object, so I could do this:

 jQuery.noConflict()(function(){ // code using jQuery }); // other code using $ as an alias to the other library 

However, is this combination valid?

 (function($) { $(function() { // more code using $ as alias to jQuery }); })(jQuery.noConflict()); // other code using $ as an alias to the other library 

If so, is there any reason not to do it this way? And also (if this works), why not always use this method to ensure that inside our closure, $ == jQuery?

+8
closures jquery
source share
2 answers

The latter method also works - jQuery.noConflict() returns a jQuery object, which is passed as the $ argument to the function.

I see no reason not to do it this way and would prefer it to other methods.

+5
source share

I don’t understand why not, since jQuery.noConflict returns a jQuery object and is evaluated before the function is called.

+5
source share

All Articles