Bootstrap the function in the source code plus the + number

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.