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

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

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.

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

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)

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.

A simple JavaScript function which is between parenthesis [duplicate]

This question already has answers here:
How does the (function() {})() construct work and why do people use it?
(15 answers)
Closed 9 years ago.
I have downloaded an external JavaScript file and want to create a HTML5 User Interface for it. I don't understand why the JavaScript code (see bellow) initiates his main function like that.
//JavaScript Code
(function(Raphael) {
// some codes here
})(window.Raphael);
Why is that function between parentheses?
What does the "window.Raphael" mean?
This is an example of a self invoking anonymous function.
You are passing in window.Raphael into this function which is essentially "renamed" to Raphael inside the function.
That is an immediately executing function (IIFE). Meaning one thats defined and executed immediately afterwards.
In this case, its also using RaphaelJS which is an SVG library. The IIFE accepts a global variable presumably defined by Raphael that is accessible at window.Raphael

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