在IE 11中动态创建对象键(预期标识符,字符串或数字,而不是逗号问题) [英] Dynamically create object keys in IE 11 (Expected identifier, string or number, not a comma issue)

查看:45
本文介绍了在IE 11中动态创建对象键(预期标识符,字符串或数字,而不是逗号问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种能够动态创建对象键(措辞正确吗?)的解决方案。

I'm looking for a solution that creates object keys (is that worded correctly?) dynamically.

任意示例,但这适用于chrome和firefox

Arbitrary example, but this works in chrome and firefox

var weeks = {}
for(var i = 0; i < 5; i++){
    $.extend(weeks, {["week" + i] : (i * 2)}
}

//weeks = {"week0":0,"week1":2,"week2":4,"week3":6,"week4":8}

或替代任意的例子

var object = {
  ["a" + 50]: "value"
}

问题似乎根源于 [] 运算符,但我不明白这个问题是如何或为什么只出现在IE中。我没有在IE11的先前版本中测试过,但我认为问题会在那里持续存在。

The problem seem to be rooted in the [] operator, but I don't understand how or why this problem only occurs in IE. I have not tested in previous versions to IE11, but I would assume the problem would persist there aswell.

由于问题似乎与 [] 运算符本身有关,在变量中创建我的键然后将该变量推送到我的[]中不会做任何事情来解决问题,所以我似乎都是谷歌的想法和关键词。

Since the problem seem to be with the [] operator itself, creating my keys in a variable and then shoving that variable into my [] wouldn't do anything to fix the problem, so I seem to be both out of ideas and keywords to google.

那么有没有办法在IE中动态创建对象键?

So is there a way to dynamically create object keys in IE?

推荐答案

IE11不像Chrome,Firefox甚至Edge那样是一个现代的网络浏览器。它不支持ES6(ES2015)的新对象文字扩展。

IE11 is not a "modern" web browser in the same way Chrome, Firefox or even Edge is. It doesn't support the new "object literal extensions" from ES6 (ES2015).

您使用的语法称为计算属性键,您不能使用它在IE11中。你需要以老式的方式做到这一点。

The syntax you are using is called "computed property keys", you cannot use it in IE11. You need to do this the "old fashioned" way.

var weeks = {};

for(var i = 0; i < 5; i++){
    var tmp = {};
    tmp["week" + i] = i*2;

    $.extend(weeks, tmp);
}

这篇关于在IE 11中动态创建对象键(预期标识符,字符串或数字,而不是逗号问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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