{ thetop : 10 } is a valid object literal. The code will create an object with a property named thetop that has a value of 10. Both the following are the same:
obj = { thetop : 10 };
obj = { "thetop" : 10 };
In ES5 and earlier, you cannot use a variable as a property name inside an object literal. Your only option is to do the following:
var thetop = "top";
// create the object literal
var aniArgs = {};
// Assign the variable property name with a value of 10
aniArgs[thetop] = 10;
ES6 defines ComputedPropertyName as part of the grammar for object literals, which allows you to write the code like this:
var thetop = "top",
obj = { [thetop]: 10 };
console.log(obj.top); // -> 10
chrome测试:
ES5
https://www.douban.com/note/625698916/
本文介绍了ES6中计算属性名称的功能,该功能允许开发者使用变量作为对象字面量中的属性名。文章对比了ES5及之前的版本,并通过示例展示了如何在ES6中利用这一特性简化代码。
5618

被折叠的 条评论
为什么被折叠?



