Using 'this' in default parameters
Tab Atkins Jr.
jackalmage at gmail.com
Tue Feb 9 19:24:26 UTC 2016
On Sun, Jan 31, 2016 at 3:59 PM, Bergi <a.d.bergi at web.de> wrote:
> Jason Orendorff wrote:
>> On Fri, Jan 29, 2016 at 8:14 AM, ` Mystery . <mystery.wd at gmail.com> wrote:
>>> IMHO I don't think the default parameters should be evaluated within the
>>> context of the function being called, at least not while it's outside of
>>> function body's region...... Is there a specific reason to do that, or
>>> just a design preference?
>> Sure, there is a reason: it's about how defaults are used.
>> Most defaults are probably constants or empty objects. Those don't
>> need to refer to any variables at all, so we can set them aside.
> Well, primitive-valued constants are fine. But if you have an empty object
> constant, which is statically attached to the function instance instead of
> being re-evaluated at every call, that would surely cause enough havoc
> already :-)
I was about to send almost this exact reply. ^_^ This is actually
*very* important, and can't be set aside - Python got it wrong, which
is why if you want an argument to default to an empty list, for
example, you have to do:
# or any sentinel value, but None is almost always usable
if bar is None:
bar = 
Otherwise you get a single array created at function definition time
and shared by all calls that default that parameter. ^_^ JS made the
right ergonomic decision for this, but of course some use-cases are
negatively affected in the trade-off, but none of them are made
impossible, or even difficult, as several people in this thread have
More information about the es-discuss