javascript - 什么时候应该使用对象属性简写?
问题描述
我知道当对象键和值相同时使用它(即,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。
有关详细信息,请参阅
推荐阅读
- amazon-web-services - 在使用 Amplify 集成创建 AWS Next.js React 应用程序时,我似乎永远无法从 amplify cli 为我创建 aws-exports.js 文件
- javascript - text/html blob 是 iframe 的来源,其中包含 html 和 JS 代码,会创建 mimetype 错误
- python - 列表索引必须是整数或切片,而不是我程序中的 str
- mongoose - MongooseError:操作 `blacklistScema.find()` 缓冲在 10000 毫秒后超时
- push-notification - Ionic 3 与 OneSignal 不兼容吗?
- flutter - 如何使用颤振获得通话帐户权限?
- firebase - 由于达到 Firestore 限制而扩大规模时
- python - 试图运行模块,但弹出此语法错误
- c - 按住一个键,然后按另一个键,停止按下原来的键
- cloudflare - 我可以在 Cloudflare 页面上托管静态 .json 文件吗?我想通过 REST 上传它们