JavaScript如何使用变量设置对象的键名 /
Posted 幸福的目标
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript如何使用变量设置对象的键名 /相关的知识,希望对你有一定的参考价值。
{ 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/
以上是关于JavaScript如何使用变量设置对象的键名 /的主要内容,如果未能解决你的问题,请参考以下文章
如何根据javascript中的键合并和替换两个数组中的对象?