Introduction
William Shakespeare once said that “What’s in Name?” but as a developer, we can disagree. We have to name variables, classe...
For further actions, you may consider blocking this person and/or reporting abuse
A lot of confusion and incorrect information in this article. Many of your 'anonymous' functions are not anonymous functions at all
If you read the article, it's for storing the function in a variable.
This is not correct - you can declare a named function without a function statement, and immediately invoke it:
It is true that you cannot immediately invoke a named function created with a function statement, but not true that you cannot invoke a named function immediately after declaration.
Immediate invocation is possible because the preceding code is an expression whose value is a function - it has nothing to do with whether the function is anonymous or not. If you have defined a function using a function statement - then that statement is not an expression and therefore has no value that you can call.
Considering this, I like to thank you as I was not having the knowledge on this. I have updated the article as per your feedback.
Yes, but the very act of assignment makes those functions cease to be anonymous functions. They acquire the name of the variable/constant. This can be shown by checking the function's name:
Contrast this with an actual anonymous function:
After the initial declaration of the anonymous function, it can't be invoked later. For that, we have to store it inside a variable. That's I did above. This has led to having a name.
Again, this isn't really related to anonymous functions. To use ANY function (anonymous or otherwise) later - we need to store a reference to it somewhere - it doesn't matter if it is named or not.
It's also perfectly possible to store anonymous functions without them acquiring a name (so they retain their anonymity) - for example in an array:
It retains anonymity but still, you need to store it into a variable.
Not having a name has no bearing on this. It's equally possible to pass in a function expression that does have a name. This may actually be preferable sometimes as it may aid the reading of debug traces in some environments (where the name will be used for display purposes).
Definitely you just need to pass function it can be annonymous, named or arrow. But annonymous will be easy to implement. It will be mostly one time function, you can go with functions not having name.
There are a number of mistakes in this example. Firstly - it won't run because
myFunction()
should say justfunction
orfunction myFunction()
. Further,console
is misspelled and the message will display after 1 second, not one minute.Typo fixed.