In JavaScript it is possible to do:
var a = {this: this}
but with ES6 property shorthand I get SyntaxError:
var b = {this}; // SyntaxError: this is a reserved identifier
This is not a real use case but I am just wondering what is the difference between these two. I thought it should do the same (either create a new object or throw an error).
UPDATE:
I run this example in Firefox 42.0. However it works in babel-node (it creates object { this: {} }
without error). So what's the correct behavior?
The grammar for that shorthand property initializer clause stipulates that the single term used must be an Identifier. Because this
is a reserved word, it isn't an identifier, so you get a syntax error.
The relevant part of the spec is section 12.2.6.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With