3 ms·
const foo = () => {} This function is not anonymous, it's called foo.
by medstrom 2y ago
const foo = () => {}
This function is not anonymous, it's called foo.
- BlarfMcFlarf 2y agoDoes the function know it’s called foo for tracing/error logging/etc?
- croes 2y agoBut to call foo in bar you must define foo before bar. function foo(){} is also callable if bar is defined before foo.
- sestep 2y agoNot true at the top-level.
- wruza 2y agoNot sure what you find not true about it. All named “function”s get hoisted just like “var”s, I use post-definitions of utility functions all the time in file scopes, function scopes, after return statements, everywhere. You’re probably thinking about const foo = function (){} without its own name before (). These behave like expressions and cannot be hoisted.
- MetaWhirledPeas 2y ago> I use post-definitions of utility functions all the time in file scopes, function scopes, after return statements, everywhere I haven't figured out if people consider this a best practice, but I love doing it. To me the list of called functions is a high-level explanation of the code, and listing all the definitions first just buries the high-level logic "below the fold". Immediately diving into function contents outside of their broader context is confusing to me.
- wruza 2y agoI don’t monitor “best” practices, so beware. But in languages like C and Pascal I also had a habit of simply declaring all interfaces at the top and then grouping implementations reasonably. It also created a nice “index” of what’s in the file. Hoisting also enables cross-imports without helper unit extraction headaches. Many hate js/ts at the “kids hate == and null” level but in reality these languages have a very practical design that wins so many rounds irl.
- mrighele 2y agoInteresting, it seems that the javascript runtime is smart enough detect this pattern and actually create a named function (I tried Chrome and Node.js) const foo = () => {} console.log( foo.name ); actually outputs 'foo', and not the empty string that I was expecting. const test = () => ( () => {} ); const foo = test(); console.log( foo.name ); outputs the empty string. Is this behavior required by the standard ?
- svieira 2y agoYes, in great detail. https://tc39.es/ecma262/multipage/ordinary-and-exotic-objects-behaviours.html#sec-setfunctionname https://tc39.es/ecma262/multipage/ordinary-and-exotic-object... is the specification, and for the TL;DR https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... is pretty good.
- Izkata 2y agoYou're probably remembering how it used to work. This is the example I remember from way back that we shouldn't use because (aside from being unnecessary and weird) this function wouldn't have a name in stack traces: var foo = function() {}; Except nowadays it too does have the name "foo".
- mapcars 2y agoNot really, its an anonymous function stored in a variable foo