jQuery and Twitter Bootstrap function explanation

后端 未结 3 1773
無奈伤痛
無奈伤痛 2021-01-07 01:55

If you see the twitter\'s bootstrap application.js file that exists in all documents, you will notice something the following code:

!function( $ ) {
    ...
         


        
3条回答
  •  清酒与你
    2021-01-07 02:13

    This guarantees that in the code inside the function you will be able to use the $ shorthand way of accessing jQuery. In some environments (Wordpress for example), they don't have the $ shorthand "enabled" to avoid conflict with other javascript libraries. By using this way here, you can use the simple $ construct in any environment (as long as window.jQuery is defined).

    So, basically it creates a function and immediately calls it, passing in window.jQuery. That means code in the function will see the $ local variable, and it's been assigned window.jQuery.

提交回复
热议问题