何时应使用对象特性速记? [英] When should I use object property shorthand?

查看:40
本文介绍了何时应使用对象特性速记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道它在对象键和值相同的情况下使用(即cat: cat可以改写为cat).但是当我查看代码示例时,我搞不清哪些情况下它们的键和值彼此相等。

let cat = 'Miaow';
let dog = 'Woof';
let bird = 'Peet peet';

let someObject = {
  cat,
  dog,
  bird
}

console.log(someObject);

推荐答案

来自文档:

ECMAScript 2015中的新符号1

在不支持的环境中,这些表示法将导致语法错误。

// Shorthand property names (ES2015)
let a = 'foo', b = 42, c = {};
let o = {a, b, c}

// Shorthand method names (ES2015)
let o = {
  property(parameters) {}
}

// Computed property names (ES2015)
let prop = 'foo'
let o = {
  [prop]: 'hey',
  ['b' + 'ar']: 'there'
}

这些表示法不适用于Internet Explorer。

有关详细信息,请参阅

这篇关于何时应使用对象特性速记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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