JQuery - no conflicts or closures

I am writing an application while using jquery. It fits into an existing application that uses a prototype, so obviously I can't use the dollar sign right away.

I use noconflict to get around this, but can't just wrap this whole script inside a closure passing in jquery, like in:

(function( $ ){ })( jQuery ); 

It seems to work, but I wonder if there are any consequences to this?

+4
source share
4 answers

This should work fine, since your passing jQuery to your closure as the jQuery object itself.

NoConflict must still be specified, otherwise the jQuery object could take control of the dollar sign. The noConflict call passes the dollar sign back to everything that already uses it.

The documentation also states the following:

  $.noConflict(); jQuery(document).ready(function($) { // Code that uses jQuery $ can follow here. }); // Code that uses other library $ can follow here. 
+1
source

you can use both:

 (function( $ ){ })( jQuery.noConflict() ); 
+6
source

This will work like a charm until you need to access the variables that are defined inside the block.

Jquery plugins are usually encoded using this exact escaping construct.

Use this instead of noConflict if you can, because noConflict forces you to pay much more attention to ordering a script on your page.

You will need to load the prototype after jquery so that it can capture the $ sign.

0
source

noConflict () resets all variables that jQuery has set with their original values. Instead of saying:

 $(document).ready() 

you just say

 jQuery(document).ready() 
0
source

Source: https://habr.com/ru/post/1341424/


All Articles