Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 688
madox2 Avatar asked Dec 25 '15 23:12

madox2


1 Answers

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.

like image 63
Pointy Avatar answered Sep 20 '22 06:09

Pointy