4 ms·
Interesting, it seems that the javascript runtime is smart enough detect this pattern and actually create a named function (I tried Chrome and Node.js) con
by mrighele 2y ago
Interesting, 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".