+ 2
What makes a function anonymous in JavaScript?
Are constructor functions anonymous
6 Respostas
+ 5
Anonymous functions are the ones without a name that cannot be called.
var anon1 = function(){};
//This function does not have a name, you can only call it if you assign it in a variable like so.
Also, arrow functions are always nameless.
+ 1
To add to Valen.H. ~ 's great answer, you can call a nameless function without assigning it but you can't reuse it!
function(){}(); should work
edit: no it don't!
but (() => alert("toto"))() does!
0
Ok.Thanks a lot
0
If that is the case,can you please explain this to me..why we call Drive() but calling person() returns an error.we only call person like ().
https://code.sololearn.com/WYhL5awtmEuj/?ref=app
https://code.sololearn.com/W9NqzL3q1xWp/?ref=app
0
Thanks for the solution ..I'll try it even though I'm still trying to wrap my head around your answer
0
Sorry I didn't read your comment. To answer you, the error is that you don't call person, you assign the result of a "function(){}()" like expression to person, but this expression is invalid. You can store function(){} in a variable then call it.
To sum it up
function(){}
- can be stored
- can be used only if stored
() => {}
- can be stored
- can be called even if not stored