(function(){})() Is there any documentation of this? [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How does an anonymous function in JavaScript work?
What does this JavaScript snippet mean?
I use in my scripts:
(function(){
...[code]..
})()
I can't find documentation about this, i have see scripts when this form take args.
(function(){
...[code]..
})(arg1,arg2)
Somebody have a link or a good explanation about this javascript function?

this is just regular javascript.
you instanciate an anonymous function, and then call it with 2 arguments.
The confusing part I think is the on-the-fly aspect of the operation.
You could have done (at higher cost):
var hnd = function() {...};
hnd(arg1,arg2);

It's known as a Self-Executing Anonymous Function.
Here is the first Google result, which gives a solid overview.

Related

How to see the code of a javascript function? [duplicate]

This question already has answers here:
How can I read ‘native code’ JavaScript functions?
(3 answers)
Closed 3 years ago.
This may be a very dumb question.
I want to see the code of a function (Built in and User defined) in Javascript.
For example :
function hello(){
console.log("hello")
}
hello.toString() // Gives the function definition
'function hello(){\nconsole.log("hello")\n}'
Is there a way to see the native code like Math.random.toString()?
Update: From the comments, Seblor explained that native code cannot be seen.
You could do some string formatting to get a "better" look at your functions. Use this peace of code to get rid of the function name to get just the code.
function justGetCode(funcName)
{
var tempString = funcName.toString();
tempString = tempString.substring(tempString.indexOf("{"));
return tempString
}
But beyond this there is little you can do in terms of digging into native (i.e. browser specific ) code as it is encapsulated. This should work on library functions however.
Now i do not know what you are planning on doing with the returned function, but for fancier function manipulation you can always use in-built reflection mechanisms

What does "+" means in +function($)? [duplicate]

This question already has answers here:
JavaScript plus sign in front of function expression
(4 answers)
Closed 5 years ago.
I had look at this question to know about what this means.
(function($) {
})(jQuery);
I am looking at different bootstrap plugins, which have +function ($), while defining the function.
What does + does here, does it appends this function to other functions ?
To guide the javascript parser, that the thing written near the unary operator + is an expression.
EDIT: You can create javascript functions anonymously. This is one of the syntaxes to create the same. By doing so, when they are called (i.e evaluated), they act like a returning a function value. You can read more from the second link which provides a good description of it.
This link explains it well
Once declared, if not named, these can be executed inline like IIFE (Immediately invoked function expressions) as well. And in this form, they can then be used to create plugins or used as namespaces and attached to window object or jquery object for use later.
Good sample file to see anonymous function code in action
http://www.programering.com/a/MTMwITMwATk.html
It's a bang function
the + operator is faster than usual !
see more at
javascript function leading bang ! syntax

What is `(function(document) { ... }(document));` [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 5 years ago.
To be honest, firstly I thought I could easily find answer in Google, but strange, there are very little information about it.
Could anybody explain, what does this code do? What is the advantage (purpose) of using this technique?
(function(document) {
...
}(document));
This is an Immediately-Invoked Function Expression (IIFE) which is basically a function that is declared and called all in one go. It's purpose is to encapsulate logic without muddying up the global namespace (any variables that you declare in that function are only visible in the function). You can find more information here, or just Google IIFE.

How is this working !function(){console.log("hi")}() [duplicate]

This question already has answers here:
What does the exclamation mark do before the function?
(8 answers)
Closed 9 years ago.
I read this on the javascript garden site. Can someone explain how it works?
!function(){console.log("hi")}()
The 'executing' parens at the end can't be done legally after a function expression. A typical (more sensical, IMO) way of writing this is with more parentheses:
(function(){console.log('hi')})()
By prepending the ! before the function expression, the JS interpreter reads the function and then runs it. This is because of the precedence of the ! operator vs. calling a function with the final ()
Look at this answer
tl;dr it defines a function to print out 'hi' and immediately calls it.

What's the difference between these two seemingly similar closure types? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Location of parenthesis for auto-executing anonymous JavaScript functions?
I'm curious now that I have seen these two similar examples:
(function ($) {
// code
}(jQuery));
and
(function ($) {
// code
})(jQuery);
Is there any difference and if so what?
There's no difference, they do the same thing. You need the parens around the anonymous function, but whether you put the parens triggering the call within those or outside them doesn't matter. Some feel the former is more "correct" (though I've never heard a strong rationale, just Crockford's assertion that it's more clear, which I contest). The latter (in my experience) is much more common.
having a pair of bracket out side the function definition makes no sense, for now, there's no diff.
these 2 equals function(){}(jQuery);

Categories

Resources