1

i came across this piece of script in the source code of a chrome extension. it declares a function:

jQuery(function($){
 $('#actionCopy').click(function(e){
 Action.copy();
 });
});

what is that jQuery word used at the beginning of the function? and what does the dollar sign that is used as the function argument do? ( function($) )

Rob W
351k87 gold badges811 silver badges683 bronze badges
asked Oct 27, 2012 at 17:59
6
  • And you didn't even tag this with jQuery sigh. Anyway the most complete answer will be found on this link. Commented Oct 27, 2012 at 18:01
  • api.jquery.com/jQuery/#jQuery3 Commented Oct 27, 2012 at 18:06
  • "what is that jQuery word used at the beginning of the function?" Well, it's the name of a function being invoked jQuery(/**args**/) just like you'd expect for any other function. The argument passed to the jQuery() function just happens to be another function. Functions are objects and can be passed around just like any other data. Commented Oct 27, 2012 at 18:07
  • ...in a similar manner, the $ is a parameter to the function argument. When invoked it is passed a value. That value is a function. The function argument passed to $ just so happens to be the same jQuery function, so now you have two different references to the same function. jQuery === $; // true Commented Oct 27, 2012 at 18:10
  • is it the same as this? (function($){ //code })(jQuery); Commented Oct 27, 2012 at 19:07

3 Answers 3

3

Passing in a function as first argument into the jQuery constructor function is just a shortcut for .ready().

jQuery(document).ready(function() {
});

jQuery will be kind enough to pass in the jQuery object reference into the callback which you pass in for the .ready handler. That means, you can just savely access the jQuery object using the dollar sign $ within.

answered Oct 27, 2012 at 18:03
Sign up to request clarification or add additional context in comments.

Comments

0

what is that jQuery word used at the beginning of the function?

It refers to a function called jQuery (defined by the library available from http://jquery.com).

That particular function is horribly overloaded, but in this case (when it receives one argument that is a function) it means "Bind this function to the document ready event" (with some pollyfill for the benefit of browsers that don't support that event natively).

and what does the dollar sign that is used as the function argument do?

The same as a letter a or the word foo or anything else. It defines the variable name that the argument will be passed to. There is nothing special about the $ character in JavaScript variable names.

answered Oct 28, 2012 at 10:51

Comments

0

jQuery is popular javascript library. $ is the shorthand for accessing jQuery object. Passing $ in function 'function($)' will make sure that $ will not be conflicted with any other javascript library used.

answered Oct 28, 2012 at 10:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.