Object Literal Property Value Shorthand incompatible with `this`

怎甘沉沦 提交于 2021-01-27 01:57:22

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/34467262/object-literal-property-value-shorthand-incompatible-with-this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!