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.")
}())
Related
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:
Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?
(4 answers)
`this` is undefined in Dev Tools when using arrow function
(2 answers)
Closed 4 years ago.
Facing a Javascript problem that has to be simple. Going nuts.
I declare and assign a variable as usual: 'let that = this';
When running the code in the Chrome debugger, 'this' is defined and has a value, but 'that' remains undefined. I know that 'this' is fickle in Javascript, but we're speaking the line after the assignment.
Any idea of what would happen? This is in a quite simple event handler, don't see where there could be a trap.
I started using Flow and transpiling JS with Codekit. I have the impression that Chrome debugger has gone a bit unstable since, could it be related?
Many thanks!
Screenshot in Chrome debugger
This question already has answers here:
What does the at symbol (#) do in ES6 javascript? (ECMAScript 2015)
(3 answers)
Closed 6 years ago.
Have viewed
#propertyOrIdentifier // ? What does this mean and do?
used within apparent plain objects or class assignment at Questions and Answers at stackoverflow.
What is the # symbol or character in javascript? What are valid uses?
It is called a decorator.
https://github.com/wycats/javascript-decorators
Medium - Exploring es7 decorators
It doesn't have any special meaning in vanilla JS.
It's likely that wherever you are seeing it it is simply being used as a naming convention, similar to doing something like adding a preceding underscore for private variables _example
This question already has answers here:
Is the underscore prefix for property and method names merely a convention?
(6 answers)
Closed 8 years ago.
What is the difference between
prototype.somefunction()
and
prototype._anotherfunction()
I know this might be trivial, but I couldn't find it.
One has an underscore in the name. There's no difference as far as the javascript engine itself is concerned.
However, it's a common convention to name "private" methods such that they start with an underscore.
_ prefix means that method is supposed to be private, not more than that
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