-
Notifications
You must be signed in to change notification settings - Fork 13
Giving name to anonymous functions #23
Comments
This only makes sense if you think of the rewrite of |
Makes absolute sense to have function name here, but as @shapesecurity mentioned most of us would interpret |
ftr you don't need an IIFE to work around it, you can do |
I could go either way on this, but I have a slight preference for it not using the name. In the same way that let x = false || function() { };
let y = true && function() { };
let z = null ?? function() { }; don't assign the name, I don't think this should either. I feel like the only time we should use the variable name for the function name is if it's completely straightforward (e.g. |
Ugh I guess we really shouldn't ignore the existence of Given this observable equivalence for get/set invocations: let a = true;
with ({
get a() { console.log('get a'); return a; },
set a(x) { console.log('set a'); a = x; },
}) {
// this line
a ||= function(){};
// is observably equivalent to this line:
a || (a = function(){});
// not equivalent to this line (the setter is invoked):
a = a || function(){};
} then I'd expect their handling of FNI should be the same. let a = false, b = false;
with ({
get a() { console.log('get a'); return a; },
set a(x) { console.log('set a'); a = x; },
get b() { console.log('get b'); return b; },
set b(x) { console.log('set b'); b = x; },
}) {
a ||= function(){};
b || (b = function(){});
}
assert.eq(a.name, 'a');
assert.eq(b.name, 'b'); |
I don't see why the same sequence of get/set invocations should have any impact whether or not the function gets an inferred name. |
@anba My thinking is that users will think of the syntax introduced in this proposal as simple sugar for one of |
if the left side is an identifier and the right side is an anonymous function, the function should have the name, regardless of what the operator is. |
Context: babel/babel#11362
As currently spec'd, Logical Assignment does not assign a name to anonymous functions. Consider:
This currently should log
undefined
. But I think it'd be totally reasonable for this to logfoo
instead.Babel hits this issue with its transform:
That
(foo = function() {})
will assignfoo
as the name for the anonymous function. We can work around this by using an IIFE to indirect. But I don't think the name hurts here.The text was updated successfully, but these errors were encountered: