{this} object literal
Regardless of whether this
is permitted as the property name of an
object, why not use console.log(this, arguments, externalVar)
? (Out of
interest)
On 21 Apr 2017 6:01 p.m., "Michał Wadas" <michalwadas at gmail.com> wrote:
Hi.
Is there any specific reason why {this} literal is forbidden? Babel had a bug allowing this and it was pretty convenient to enter console.log({this, arguments, externalVar}) for quick printing scope.
Michał Wadas
Because the shorthand only requires an Identifer, and an IdentiferName is not sufficient. If the rule allows arbitrary IdentiferName, then we will also allow {if}
, which makes no sense it all. So if we really want to allow this, it would be a special rule in addition to the current rule. In this sense, we are not forbidden the use of {this}
, but instead we don't have the rule to allow that.
More generally, all keywords are excluded, that makes sense in general as, e.g., { if }
could not be equivalent to { if: if }
, or { yield }
inside a generator function should not be equivalent to { yield: yield }
.
But yes, one could have special-cases for literals such as this
, true
, false
and null
. It is an accident of history that { NaN }
and { undefined }
work as expected, but not { null }
.
Because the shorthand requires an Identifer, and an IdentiferName is not sufficient. If the rule allows arbitrary IdentiferName, then we will also allow {if}
, which makes no sense it all. So if we really want to allow this, it would be a special rule in addition to the current rule. In this sense, we are not forbidden the use of {this}
, but instead we don't have the rule to allow that.
Is there any specific reason why {this} literal is forbidden? Babel had a bug allowing this and it was pretty convenient to enter console.log({this, arguments, externalVar}) for quick printing scope.
Michał Wadas