什么是JavaScript速记属性? [英] What is JavaScript shorthand property?

查看:199
本文介绍了什么是JavaScript速记属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var obj = { prop = [1,2,3] };

上面的代码包含一个拼写错误,应该有冒号而不是 = 。但令我惊讶的是VM错误消息:

The code above contains a typo, there should be a colon instead of =. But what surprised me was the VM error message:

var obj = { prop = [1,2,3] };
            ^^^^^^^^^^^^^^
SyntaxError: Invalid shorthand property initializer

我搜索了JavaScript速记属性,但这个术语对我来说仍然不清楚。 速记属性在此错误消息的上下文中意味着什么?

I searched for "JavaScript shorthand properties" but this term is still not clear to me. What does "Shorthand property" means in context of this error message?

推荐答案

使用ES6,您可以使用允许的简写属性名称你写这样的东西。

With ES6, you can use shorthand property names which allow you to write something like this.

var s = 'abc';
var n = 1;
var o = { s, n }; // This is equivalent to { s: s, n: n }

在你的情况下, prop = [1,2,3] 被解析为一个速记属性( s n 在上面的示例中),但它不是一个合适的属性名称。

In your case, prop = [1,2,3] was parsed as one shorthand property (s and n in the example above), but it was not a proper property name.

这篇关于什么是JavaScript速记属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆