Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this jQuery related, and what does this mean? [duplicate]

I have been searching, but no clues... or I haven't search the proper way (so please excuse me if duplicate)

Does the following code, mean: If there is no jQuery defined, or no document ready?

!function ($) {

///

!function ($) {

  $(function(){ // I know this is an alias to $(document).ready()

  .....

}(window.jQuery) // Ending of !function

I'm asking, because I saw it here: http://twitter.github.io/bootstrap/assets/js/application.js and have no I really don't know what it means.

like image 562
Alex Avatar asked Jun 25 '13 19:06

Alex


People also ask

What is this jQuery?

$(this) is a jQuery wrapper around that element that enables usage of jQuery methods. jQuery calls the callback using apply() to bind this . Calling jQuery a second time (which is a mistake) on the result of $(this) returns an new jQuery object based on the same selector as the first one.

How can check duplicate value in textbox using jQuery?

Using each() check value of inputs and if any value is duplicate add class duplicate to it.

Is JavaScript and jQuery the same?

JavaScript is an independent language and can exist on its own. JQuery is a JavaScript library. It would not have been invented had JavaScript was not there. jQuery is still dependent on JavaScript as it has to be converted to JavaScript for the browser in-built JavaScript engine to interpret and run it.

Is jQuery replacement of JavaScript?

Since jQuery is nothing but a library of JavaScript, it cannot replace JavaScript. All jQuery code is JavaScript, but jQuery doesn't include all the JavaScript code. One thing you should understand is that they are not two programming languages; instead, they both are JavaScript.


2 Answers

In this case, ! is being used because it's an operator, so the rest of the line will be treated as an expression rather than a statement. This is a way of writing an immediately invoked function expression. The more common idioms can be found here:

Javascript immediately invoked function patterns

like image 197
Barmar Avatar answered Nov 02 '22 06:11

Barmar


! on a function(){}() simply flips (or negates) the value that's returned after immediately calling the function that's defined. Notice that immediately after the function definition, at the very last line, it says (window.jQuery) — that's passing jQuery as the argument to the function and calling it immediately.

But in this case it doesn't appear to do anything important since the return value won't be used anyway. The function will still be executed though.

Also, it says this at the top of the file:

// NOTICE!! DO NOT USE ANY OF THIS JAVASCRIPT
// IT'S ALL JUST JUNK FOR OUR DOCS!
// ++++++++++++++++++++++++++++++++++++++++++

So that's evidence further that it's not meant to serve any real purpose.

like image 23
BoltClock Avatar answered Nov 02 '22 05:11

BoltClock