Need help understanding Twitter bootstrap plugins (jQuery)

I get all the plugin code except one line, and due to how search engines work, I canโ€™t find my question.

The question is, what the hell is this:

(!function($) {/*....*/})(jQuery); 

Why is he talking! functions ($) ?? I assume that for the same effect $.noConflict() , but not because we just use (function($) {/*...*/})(jQuery); , it simply transfers the dollar sign to our function block. I fully understand that I could be outside the base here, I'm still intermediate in jQuery / js.

If someone could tell me about the effect of the NOT operator before the function operator, I would really appreciate it.

Change It seems like I forgot that bootstrap does not have (!function($) ... ) , but simply !function($)... but you guys helped me understand that this is just an alternative ()

+6
source share
1 answer

So here a couple of things happen. Everything ends with an anonymous function that runs immediately. This is done to create a scope for the library, so when the author declares functions and variables, they are not global by default.

With me still. So, the next thing is the total ($){...}(jQuery) thing. What happens here, the author passes the jQuery library into an anonymous wrapper function as a parameter named $ . Therefore, as part of an anonymous function, it can use $ for jQuery links, as one would expect. This is more suitable for practice, because 99% of the time jQuery is already defined as $ in the global scope, but its poor form refers to global variables within the scope of functions, and thus it passes it to as a parameter.

Yet? Exclamation mark: There is a rule in Javascript that says that the first line of a program MUST be an expression (i.e. Not a function declaration of aka). Adding! the author turns the declaration into an expression (which returns false). In this case, however, I do not think so! actually required because the declaration is wrapped in () and executed immediately. (A function call is an expression)

+3
source

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


All Articles