In general, the jquery plugin is written in this way
(function($) { //... }) (JQuery);
Today, I saw Bootstrap's JavaScript component written like this.
! function($) { window.jquery);
Why add a "!" in front of you?
As we all know, there are two ways to declare a function
function fnA(){alert(‘msg‘);}//声明式定义函数var fnB = function(){alert(‘msg‘);}//函数赋值表达式定义函数
The two functions that appear in the landlord problem are anonymous functions. Usually, the way we call a method is functionname ()
However, if we try to add () to the end of a defined function, the parser is incomprehensible.
function msg(){ alert(‘message‘);}();//解析器是无法理解的
The calling method of the defined function should be MSG (); So why wrap the function body part in ()?
It turns out that using parentheses to define the function body, the parser will invoke the definition function in the form of a function expression. In other words, any function that can be turned into a function expression can make the parser call the definition function correctly. and! is one, and the +-| | have such a function.
In addition, use! Perhaps more of a habit problem, different operators, performance is different.
Just to be able to omit a character ...
This will result in an error, because this is a function definition: Functions() {} ()//Common (more than one pair of parentheses), call anonymous function: (function() {})// But in front of a Boolean operator (only one more exclamation point), is the expression, will execute the following code, also legitimate implementation of the call! function() {} ()
Bootstrap the function in the source code plus the + number