3 ms·
> "If you've programmed in JavaScript, this form is equivalent to: function (v) { return e ; } " function (v) { return e ; } -> Uncaught SyntaxError: Function
by voidUpdate 5mo ago
> "If you've programmed in JavaScript, this form is equivalent to: function (v) { return e ; } "
function (v) { return e ; }
-> Uncaught SyntaxError: Function statements require a function name
function a (v) { return e ; }
a()
-> Uncaught ReferenceError: e is not defined
Am I missing something?
- emigre 5mo agoIt's meant as an example. The function receives something, which we call v, and returns something else, which we call e. It's not meant to be taken literally as the variable names - otherwise, you are right, e is undefined in that example. He is just showing how the syntax of a Scheme function corresponds to the structure of a JavaScript function.
- ramon156 5mo agoIts just an example. It doesn't matter what the function name is. And it doesn't matter where e comes from. No need to include it
- jraph 5mo agoThis 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 :-)