What does the exclamation point in front of the JS function mean? __javascript

Source: Internet
Author: User
Tags anonymous function definition
JS function before adding a semicolon and an exclamation point is what meaning. What's the use.

It looks like this in the jquery plugin.

(function ($) {         
  //...  
}) (JQuery);

The JavaScript component that I saw Bootstrap today is written like this

!function ($) {
  //...
} (Window.jquery);

Why add a "!" to the front.

As we all know, there are two ways in which functions are declared

function FnA () {alert (' msg ');} Declarative definition functions
var fnB = function () {alert (' msg ');} function assignment expression definition function

The two functions that appear in the landlord question are anonymous functions. Usually, the way we call a method is functionname ()

However, if we try to add () to the end of a "definition function", the parser is incomprehensible.

function msg () {
  alert (' message ');
} ();//parser is incomprehensible

The method of defining a function should be MSG ();

If the function body part is wrapped in () it can be run and the parser is not an error, such as:

(function ($) {         
  //...  
}) (JQuery);

So why do you wrap the function body part with ()?

It turns out that using parentheses to define the function body, the parser will call the definition function in the form of a function expression. That is, anything that can turn a function into a function expression can make the parser correctly invoke the definition function. and! is one of them, and +-| | All have such a function.

In addition, use! Probably more of a habit problem, different operators, performance is different.

These have a very detailed article, recommended reading: http://www.swordair.com/blog/2011/10/...

is to omit one character ...

This is an error, because this is a function definition: Functions
() {} ()

//Common (extra pair of parentheses), calling anonymous functions:
(Function () {}) ()

// But preceded by a Boolean operator (with just an exclamation point) is an expression that will execute the following code and invoke
!function () {} () on the legal implementation.

In front plus ~+-and other unary operators can also be ... In fact, there are several compliance guarantees that the anonymous function will be executed immediately after it is declared.

var hi = function () {alert ("HI")};
Hi ();
Equals...
(function () {alert ("HI")}) ();
!, + and () the same effect can be replaced by
!function () {Alert ("Hi")} ();
! than () save a character, or say better than ()

We all know that the semicolon is to be separated from the preceding code, JS can be separated by line break code, but after merging multiple JS files, line breaks will generally be deleted, so even together may be wrong, plus a semicolon on the insurance.

the exclamation point you see is generally in the compressed JS file, because when anonymous functions are called, we usually use:  (function () {}) ()  , but can also be in another form:!function () {} () before the! can be replaced by the-+~ and so on unary operators, thus saving 1 of bytes.

SOURCE reference:

http://segmentfault.com/q/1010000000117476

Http://zhidao.baidu.com/link?url= Tzrm5eim5yt6cpeb-w9i0niplfrh19kwfkj6hcjsty863ty2w2uburgx1ocbjvhxnqyxpdufkunts2a_72st-npomvo5phoko88ksvwdrbu

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.