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.
Related
This question already has answers here:
How does this JavaScript/jQuery syntax work: (function( window, undefined ) { })(window)?
(5 answers)
Closed 8 years ago.
I am using my js files like:
(function() {
'use strict';
angular
.module('app.someModule')
.config(config);
function config(someDependency){
//some configuration
}
config.$inject=['someDependency'];
})();
However I saw when i using closures some people injects the angular object itself the closure. Something like:
(function(angular){/*whatever logic*/})(angular);
Which one is a better usage or Are there any difference between two usage?
So I dont add global angular variable as always that don't cause any trouble?
The difference between two immediately executed functions (IIFE) is that in the second case you invoke function with one parameter angular. The benefit it can give is slightly improved performance as there is no need for Javascript engine to look up for a variable in global scope, as the angular object is available in local closure scope as a reference passed with function invocation (but of course it still points to the same Angular object, defined in global scope).
This question already has answers here:
What is the purpose of a self executing function in javascript?
(21 answers)
How does this JavaScript/jQuery syntax work: (function( window, undefined ) { })(window)?
(5 answers)
Closed 9 years ago.
I don't understand what the last line of code is doing here. It looks like random parenthesis tagged at the end of the function. I don't understand the syntax.
(function (self, $, undefined) {
self.methodName = function () {
//do stuff
}
})(This.IsTheNameOf.MyJsFile, Jquery);
What I do know: self = namespace organization tool. $ = JQuery. The first thing in the last line of code is the name of the JS file that contains this code. The last line obviously isn't a function call, but it seems to coincide with self and $.
Any knowledge is greatly appreciated!
Leaving out some stuff we have
function (self, $, undefined) {
// ...
}
So, basically, a function (though a name is missing). Now this gets wrapped in
(/* above code here */)(...);
This is a so-called IIFE (Immediately-Invoked Function Expression). In other words: The function is created and immediately invoked. The reason for this is that it creates a scope in which you can have "private" variables. Also, jQuery gets aliased to $ inside that scope for easy reference. Similarly, This.IsTheNameOf.MyJsFile gets aliased to self.
If you look closely, the function expects three arguments, but is only called with two. This forces the last argument to be (the native) undefined, which happens to be the name of that parameter inside the IIFE. This ensures that undefined, inside that scope, has the expected value (older browsers allowed to overwrite it).
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.
This question already has answers here:
How does this JavaScript/jQuery syntax work: (function( window, undefined ) { })(window)?
(5 answers)
Closed 9 years ago.
(function (global, undefined) {
... some code which doesnt use arguments array
} (this));
I often see module pattern done in this way.
I really question why there's a second argument undefined?
Are these examples buggy or is there a special meaning of undefined here?
undefined is a global property that is widely used. In older versions of JavaScript it is possible to change the value of it (for example, to true). This generally breaks everything.
By changing its scope to be local to the "module" (i.e. the function), other modules are prevented from interfering with it.
This allows code to safely use undefined instead of having to use global.undefined.
MDN Reference
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