This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
JavaScript: immediate function invocation syntax
A similar question has been asked here
there, the author asked about the difference between
(function(){})();
and
function(){}();
the purpose of the first is, among other things, to inform the reader that the function is to be immediately executed. I have also seen this version
(function(){}());
Is this the same as the above two?
This variant function(){}(); will give you syntax error
Related
This question already has answers here:
How do JavaScript closures work?
(86 answers)
Closed 2 years ago.
I have been learning javaScript lately but I got confused in understanding the concept of closures.How does JavaScript closures actually work ?
A closure gives you access to an outer function’s scope from an inner function
for more info you can follow this link-
http://sleeplessgeek.blogspot.com/2009/12/so-what-are-these-closure-thingys.html
This question already has answers here:
What does this.async() do in JavaScript
(3 answers)
Closed 3 years ago.
This might be a possible duplicate of this question What does this.async() do in JavaScript but that question is under a different circumstance and the accepted answer got negative vote, hence wrong or original question unaddressed. Also the most liked answer did not clarify it, the answerer also said so in his answer.
What is the actual use of this.async(); is JS? Is it part of the core JS language or a conventional function used by libraries? I couldn't find any documentation on this.
The this.async() is not standard (core) JS function
this.async();
If anybody else is wondering about this. I just found this link which kind of sums it up and details the use case.
https://github.com/sboudrias/run-async#readme
And Grunt also use this same technique to know that an async task has finished.
https://gruntjs.com/inside-tasks
This question already has answers here:
Location of parenthesis for auto-executing anonymous JavaScript functions?
(4 answers)
Closed 5 years ago.
In a Javascript programming community, someone asked "How to declare anonymous method and run it immediately?" then others answered (function(){})() and (function(){}()).
People who answered (function(){}()) says that is correct and it's possible to run, but ideone says that is wrong and I think that is incorrect.
Following links are for comparison between SpiderMonkey and Rhino.
http://ideone.com/rKaYtW
http://ideone.com/Lblb7w
Is (function(){}()) really correct on Javascript?
"but ideone says that is wrong"
If you are talking about the error in the links you show, that's because you didn't separate the two examples with a semicolon, so the second one is having its outer set of parentheses interpreted as a function call of the previous expression.
(function(){
print("This is what I want.")
})() // <<< no semicolon causes the error here
(function(){
print("This may be what I want.")
}())
This question already has answers here:
How do you find out the caller function in JavaScript?
(36 answers)
Closed 9 years ago.
I have a common error function which is called from many places. I want to know from which function this was called. Is there any way to do it?
In any javascript function you can use
arguments.callee.caller
if You want to execute JS in chrome you have to do like this
go to console and type your JS and enter!
This question already has answers here:
Why can I use a function before it's defined in JavaScript?
(7 answers)
Closed 10 years ago.
I have noticed that when coding in javascript, it doesn't matter if i declare the function before or after i call it. In other languages it will cause an exception, but when running javascript in chrome it works just fine. Is that only in chrome, or is that normal?
What you're seeing is function hoisting in action: http://elegantcode.com/2011/03/24/basic-javascript-part-12-function-hoisting