First you learn the syntax to create a function, and that's fine. Then you start hearing about anonymous functions, and they look a bit different a...
For further actions, you may consider blocking this person and/or reporting abuse
Thanks so much for this guide. Very helpful for a beginner like I.
I have a few questions though.
I found wording is a bit confusing in the last 2 code snippets. You are converting a function named brag to an anonymous function yet from the code it looks like the function was anonymous to begin with.
And the one that I'm more curious about, is there a way to do the same conversion but using an arrow function syntax instead? I tried it and keep getting the
SyntaxError: missing ) after argument list
Is there a rule against this kind of convention?
@mail2lawi You are 100% correct! I meant to write a named function, but I wrote an anonymous function instead. I have now corrected it, thanks to you!
here the IIFE syntax is incorrect, i.e. the round brackets around the arrow function are missing
Arrow functions are more (and less) than just a shorthand to anonymous functions; which make then useful in particular cases. They preserve the context of
this
, which solves some really confusing scoping issues that previously required closures orbind(this)
.There's some discussion of this in the MDN docs
Very good answer
Great point, thanks!
I think this article really needs an update. because for people who didn't read Ben Calder's comment. the takeaway might be "Arrow functions are just shorter alternatives to anonymous expressions."
additional information on how arrow function is different here: dmitripavlutin.com/when-not-to-use...
Thanks for the nudge, @angela! I've updated the post.
Another difference on arrow vs anonymous functions I've found: it seems arrow functions are not constructors. Therefore
fnC=() => {}; c=new fnC();
will error withfnC is not a constructor
. While an anonymous functionfnB = function() {}; b=new fnB();
will instantiate an empty{ }
object.Is anonymous function the same as function expression?
Yup!
There is a difference between arrow function and anonymous function...the explanation is not right....
Care to elaborate?