Why need to pass jQuery object as parameter to closure? [duplicate] - javascript

This question already has answers here:
Why define an anonymous function and pass it jQuery as the argument?
(5 answers)
jQuery dollar sign ($) as function argument?
(4 answers)
Closed 9 years ago.
To avoid global variables in JavaScript code I many times seen that people use this construct:
(function($) {
// here code
// here code
})(jQuery);
So I have questions: why we need to declare function argument as $ and why we need to pass jQuery object as argument?

You don't have to. You can name the function argument whatever you want it to be. It is just a common/best practice of people using jQuery since it is common to use $ as an alias to jQuery library object.
The reason why you should do it is because there are other libraries that use $ as an alias to their library objects. It is needed to avoid collisions with those libraries since function closure will ensure $ to be jQuery object inside the wrapper function.
Here's an example:
(function (myJqueryAlias) {
console.log(myJqueryAlias('document') === jQuery('document'));
})(jQuery);

With this you archive that in this block you still have $ as jQuery and you can still write stuff like "$("selector")" instead of "jQuery("selector")".
And outside this block the variable $ is still what it should be (for example you use another JSLib that uses the $ too)

Related

What does function($) mean? [duplicate]

This question already has answers here:
What does function($) mean in javascript?
(3 answers)
Closed 9 years ago.
I just started learning JavaScript. Found a statement function($) { ...} while checking out examples. Can anyone tell me what function($) means?
It means "This defines a function. When it is called: create a local variable called $ and assign the value of the first argument to it."
First off: This creates a function where $ is the first argument passed to it. It could look like this potentially:
function dollar($){
alert($);
}
dollar("hello")
//alerts "hello"
Typically this is used when you want the $ to mean jQuery.
For example:
(function($){
//stuff where $ === jQuery
})(jQuery)
Means that jQuery will be passed into the $ variable for the scope of anything that happens in that function.
This can be useful if you have multiple libraries within the global scope that may use the $ variable, but you have a modular plugin that refers to the necessary library as that and you don't want to rewrite the whole thing.
Note: It does not always mean jQuery but in about 80% of cases it will. Otherwise it is just a convenient way to bind a library to a shorter variable within a certain scope.

what does the keyword 'jQuery' do in this js script [duplicate]

This question already has answers here:
jQuery dollar sign ($) as function argument?
(4 answers)
Closed 9 years ago.
I'm looking through someone else's javascript when I came across the jQuery keyword used like this:
(function ($) {
//stuff to do
})(jQuery);
jQuery(document).on('pageshow', function () {
//call functions
});
what does the jQuery keyword exactly do? or am I reading this wrong and the word 'jQuery' is just an object that is returned from the script or something?
You are aliasing the jQuery global variable to $ in case it was already defined.
In this case the jQuery variable represents the global variable for the jQuery library that was most likely loaded in a script tag before in the HTML code.

what's mean: !function in javascript? [duplicate]

This question already has answers here:
What does the exclamation mark do before the function?
(8 answers)
Closed 9 years ago.
I have a simple question.
I've found this code, and i don't know this statement
!function ($) {
// (...)
}(window.jQuery);
why put ! before a function?
i've found this on bootstrap.js file, and i really want to know.
Thanks!
It is a duplicate as nnnnnn mentioned. What the code is doing is executing the anonymous function while passing window.jQuery as a parameter, which will be referenced as $ inside the function. This allows the use of $ to reference jQuery without conflicting with any other library that might use the dollar sign.
This is a more readable version of the code:
(function($){
// here, $ references jQuery and any variable or function
// declared here cannot be overridden outside of this function
})(window.jQuery)
The ! will always parse the statement as being true if the statement is not able to be parsed.
You can see this by using,
javascript:alert(!function(){}())
Where the resulting response is true

What is this syntax? ; (function ($, undefined) [duplicate]

This question already has answers here:
What does (function($) {})(jQuery); mean?
(6 answers)
Closed 9 years ago.
; (function ($, undefined)
{
// all the variables and functions of the js document
})(jQuery);
I've seen this twice now in the jquery/javascript files for a zoom script. I don't understand what this is exactly. I can't seem to google it, I don't remember coming across this on tizag or w3schools while recently learning jquery and js.
There's nothing before or after this code (other than some comments). So I'm utterly lost as to what (function())(jQuery); is or does.
(function ($, undefined)
{
// all the variables and functions of the js document
})(jQuery);
calls a block of code ensuring that inside
$ is usable to refer to jQuery
undefined is undefined (edit: this was useful because undefined could be redefined at that time in the oldest browsers, it's now useless)
and that any minifier can change undefined to a shorter label.
The initial ; ensures you can concatenate this file with another one : without this, you'd have an error executing the concatened file if the one just before was something like
(function (){
})()
This is a way to ensure that $ is indeed the jQuery object and to ensure that any local variables and methods are privately scoped, that is, do not pollute the global namespace.
It is a self-calling anonymous function, with the parameter passed being jQuery, meaning that the $ will be the jQuery object.
Being declared inside a function means that the inner variables and methods will not be visible outside of it.

jQuery's function($) (jQuery) syntax [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
jQuery: What does (function($) {})(jQuery); mean?
I stumbled up on the following code (included in one file) but I just cannot understand what it really means.
(function ($) {
function doSomething1(somedata) {
}
function doSomething1(somedata) {
}
})(jQuery);
Question 1:
What does this syntax mean in the contex of jQuery
Question 2:
How can we call these functions let say from other files such as the HTML index file and other JavaScript files?
Thanks
This syntax is not particularly special to jquery, it's normal javascript. Here simply function
function ($) {
// some code here...
}
(note that it takes argument named $) is called with parameter jQuery (which is, obviously, global object of jQuery framework).
This is usually done when there're several js frameworks on one page (jquery, dojo, prototype, etc) which all redefine global variable $. But with this code, inside doSomething1 or doSomething2 you can always call $('.test') and be certain that call will be processed by jquery, not dojo. Because $ is not a global variable in this case, it's function parameter.
I'm not sure about your question here, but the (function() means it's self-executing,
and you call them by importing the files in the main page and then calling
doSomething1()
Mostly likely that was jQuery plugin: Plugins/Authoring

Categories