madox2
madox2

Reputation: 51841

Object Literal Property Value Shorthand incompatible with `this`

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?

Upvotes: 10

Views: 406

Answers (1)

Pointy
Pointy

Reputation: 413702

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.

Upvotes: 8

Related Questions