首页 > 代码库 > JavaScript如何使用变量设置对象的键名 /
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如何使用变量设置对象的键名 /
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。