3 ms·
This is a statement vs expression thing. It works as an expression. Try: f = function (v) { return e ; } or (function (v) { return e ; }) Javascript
by jraph 5mo ago
This is a statement vs expression thing. It works as an expression. Try:
f = function (v) { return e ; }
or
(function (v) { return e ; })
Javascript doesn't allow (nameless) function expressions (using the function keyword) where a function statement would work.
(and that particular function can also be written v => e)
As for the e being undefined when you execute the function, you should see it as a free variable [1], supposed to be defined elsewhere or replaced with something else.
[1] https://en.wikipedia.org/wiki/Free_variables_and_bound_variables https://en.wikipedia.org/wiki/Free_variables_and_bound_varia...
- mattmight 5mo agoGood point -- JavaScript's syntax grew up since I wrote that article!
- jraph 5mo agoIndeed! And since your article is not particularly about JS and can be read by anybody, including people not familiar with (modern) JS, I'd argue that the form you used is way clearer than the arrow function form and would probably still be a better choice should you write this article today :-)