0
<script>
jQuery(function($) {
    $('#container').jstree();
});
</script>

$ is passed into anonymous function, what does it mean?

StuperUser
  • 10,555
  • 13
  • 78
  • 137
hywl51
  • 551
  • 1
  • 5
  • 13
  • possible duplicate of [What does (function( $ ){...})( jQuery ); do/mean?](http://stackoverflow.com/questions/2464635/what-does-function-jquery-do-mean) – JJJ Oct 02 '13 at 12:08
  • Does this answer your question? [explain this javascript function declaration "jQuery(function($){}"](/q/13102699/90527) – outis Oct 10 '22 at 21:57

9 Answers9

2

jQuery Documentation

A dollar sign ($) is actually an alias for jQuery function. And according to the documentation, if you pass a callback as an argument to this function, it will be executed when the DOM is ready.

2

Read Documentation

jQuery(function( $ ) {
  // Your code using failsafe $ alias here...
});

and

$(function() {
  // Document is ready
});

Use both the shortcut for $(document).ready() and the argument to write failsafe jQuery code using the $ alias, without relying on the global alias.

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
1

It makes "$" the local variable and thus gracefully avoids the conflicts with any other variables which possibly use "$" symbol.

These function all do the same things - execute some code when DOM is ready.

"$" and "jQuery" which accept function as an arguments were created as shortcuts to avoid repeating such a common construct. Accepting a function which accepts $ as its first argument is a further syntax sugar - now you get convenience of the closures without the need to do it yourself.

Bindiya Patoliya
  • 2,726
  • 1
  • 16
  • 15
1

http://api.jquery.com/jQuery/#jQuery3

jQuery(function( $ ) { // Your code using failsafe $ alias here... });

Alexander Taran
  • 6,655
  • 2
  • 39
  • 60
0

$ is an alias of jQuery.

So you could also write jQuery('#container').jstree();

MildlySerious
  • 8,750
  • 3
  • 28
  • 30
  • It's worth noting that `$` is just a valid variable name in javascript – like `a` or `b` – not something intrinsically magical. That was not obvious to me years ago when I was first learning. – J. Holmes Oct 02 '13 at 12:06
  • Also worth noting that the question is about the anonymous function, not its contents. – JJJ Oct 02 '13 at 12:14
0

The code looks like an attempt at the Module Pattern in JavaScript. The benefit is that the anonymous function has a closure that helps to maintain the privacy of variables within it and maintains the state of its local variables and functions.

The correct way to implement the pattern is:

(function($) {
  ...
 })(jQuery);

where the (...)() is an example of an IIFE (Immediately Invoked Function Expression). The function expression is invoked by the trailing (), which if it contains jQuery will pass the jQuery function into the anonymous function's scope as $, since that is what it has as the parameter name. A benefit of this pattern is people can write jQuery plug-ins and can maintain their own copy of the jQuery function and variables and call them whatever they want, which helps resolve name conflicts.

The currently accepted answer is incorrectly interpreting the documentation, which is merely stating that if you have a function declaration, you can execute it as a callback when the document is ready by passing it to the jQuery function, which is equivalent to jQuery's $(document).ready(function(){...});

Community
  • 1
  • 1
Alex W
  • 37,233
  • 13
  • 109
  • 109
-1

JQUERY DOC :

jQuery() — which can also be written as $() — searches through the DOM ...

Thus :

$( "div.foo" ); is equal to jQuery( "div.foo" );

S.Thiongane
  • 6,883
  • 3
  • 37
  • 52
-1

In short: $ refers to a global variable that is set by JQuery to represent itself.

In not so short: Javascript variable naming conventions allow you to start a variable with '$', '_' or any valid unicode character in the categories “Uppercase letter (Lu)”, “Lowercase letter (Ll)”, “Titlecase letter (Lt)”, “Modifier letter (Lm)”, “Other letter (Lo)”, or “Letter number (Nl)”.

Therefore you are allowed to have the variable $ or _ on it's own. A lot of javascript frameworks/libraries/things exploit this fact to shorten their calls.

UndeadKernel
  • 515
  • 4
  • 15
-1

So in JQuery (Javascript library), the dollar sign is an alias/reference to a JQuery function.

In this case, the first line is equivalent to $(document).ready(), which waits for the DOM to finish loading before running.

Another words, functions you want to run after the DOM has finished loading (almost all functions really) you want to put inside this, as jstree() is.

Here is a relevant S.O. question

Here is a link to JQuery Documentation on Callbacks

Community
  • 1
  • 1
Benji
  • 71
  • 5