-
Notifications
You must be signed in to change notification settings - Fork 2k
Description
From @GeoffreyBooth on 2016-10-19 07:15
In CoffeeScript 1.x, null
and undefined
are often interchangeable:
if foo?
returns false whetherfoo
isnull
or whetherfoo
isundefined
.((a = 1) -> a)(null)
and((a = 1) -> a)(undefined)
both return1
.
In ES2015, at least regarding default function parameters, null
and undefined
are not interchangeable:
(function (a = 1) { return a; })(null)
returnsnull
.(function (a = 1) { return a; })(undefined)
returns1
.
This has sparked a discussion in the PR where I’m trying to implement arrow functions (and by extension, ES-style default parameters) for CoffeeScript 2. Basically, if we want to preserve the current behavior where the default value is applied when a parameter is null or undefined, we need to keep the current generated output:
f = function(a) {
if (a == null) {
a = 1;
}
return a;
}
So if we want output like this:
f = function(a = 1) {
return a;
};
Then we have to break backward compatibility and go with ES’ implementation of default parameter values applying only when the parameter is undefined, not null or undefined. We can’t do both (the default value and the null
if block) because the default value could be a function call, for example f = (a = b()) -> a
.
CoffeeScript 2 was going to break backward compatibility anyway, but we want to break as little as possible. Is this null
/undefined
interchangeability something that people value in CoffeeScript 1.x, or would you not miss it if it went away? How important is it that the generated JavaScript for a feature like default function parameters be as ES-like as possible?