-1

In order to prevent prototype.js conflicts with jquery I wrapped my jquery code in the following snippet:

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

If I understood this correnctly, $ === jQuery would be true inside my function. But is the actual "in parameter" jQuery in this case, which gets the alias $ inside my function?

If my assumption is correct, do I need to pass jQuery on both places in order to call it jQuery, or would it be ok to just pass it at the end of the function?

asked Mar 28, 2013 at 13:07
2
  • $ is the parameter name, and the variable jQuery (it could be any expression) is what you pass in as an argument. Commented Mar 28, 2013 at 13:08
  • This may be a duplicate of <stackoverflow.com/q/14400210/3840170>, but it’s too vague to tell. Commented Dec 26, 2024 at 13:26

3 Answers 3

2

Within your IIFE you can use either $ or jQuery - they're both in scope.

Only the (jQuery) is actually being passed as an argument - the $ is just the function parameter (and therefore aliased to jQuery).

To see that, your code is almost equivalent to:

var f = function($) {
 ...
};
f(jQuery);

except that your IIFE is an anonymous function.

answered Mar 28, 2013 at 13:08
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, just the explanation I was looking for
1

If I understood this correnctly, $ === jQuery would be true inside my function. But is the actual "in parameter" jQuery in this case, which gets the alias $ inside my function?

Yes. $ is the parameter name, and the variable jQuery is what you pass in as an argument - it could be any expression.

If my assumption is correct, do I need to pass jQuery on both places in order to call it jQuery, or would it be ok to just pass it at the end of the function?

Yes, you would need to rename the parameter. Only it doesn't make much sense then, as you could just refer to the global jQuery variable then (unless you plan to overwrite that, e.g. with a different jQuery version) - the $ alias is only for brevity. If you want to avoid confusion with Prototype, use jQ instead.

answered Mar 28, 2013 at 13:11

2 Comments

Thank you. Followup question: If I want to access something inside the closure from the outside, would I have to return it from inside of the closure first?
Yes, either return it or assign it to something that is accessible from outside (e.g. a global variable).
0

Inside the closure, only $ reliably references the jQuery library; more specifically, the library version at that point in time.

When another version of the library is loaded afterwards, only $ still points to what you expect; the jQuery symbol would have been replaced by the latter version.

If you wish to use the jQuery alias inside the function you would need to rename $ to jQuery in the function arguments.

answered Mar 28, 2013 at 13:12

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.