javascript 边缘:SCRIPT1028:预期的标识符、字符串或数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53628191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Edge: SCRIPT1028: Expected identifier, string or number
提问by Maurits Bouman
My page works fine in Chrome and Firefox:

However, when I try to load this page in Edge, the questions and answers disappear. Only the categories are posted. Also, when trying to load this page in IE, everything disappears except for the search bar.
但是,当我尝试在 Edge 中加载此页面时,问题和答案消失了。仅发布类别。此外,当尝试在 IE 中加载此页面时,除了搜索栏之外的所有内容都消失了。
Edge gives me the following error:
Edge 给了我以下错误:
SCRIPT1028: SCRIPT1028: Expected identifier, string or number on line 84 of faq.html
SCRIPT1028:SCRIPT1028:faq.html 第 84 行的预期标识符、字符串或数字
This refers to the following code:
这指的是以下代码:
function sortByCategory(data) {
return data.reduce((obj, c) => {
const { category, ...rest } = c; // this line throws the error
obj[category] = obj[category] || [];
obj[category].push(rest);
return obj;
}, {});
}
How do I fix this?
我该如何解决?
采纳答案by T.J. Crowder
It appears (surprisingly) that Edge doesn't support property rest yet, which is unfortunate but then it was officially added only in ES2018. You'll need to rewrite the code not to use property rest (the ...restpart of your object literal) (or, as CertainPerformance suggests, use a transpiler).
看起来(令人惊讶的是)Edge 还不支持属性休息,这很不幸,但后来它才正式添加到 ES2018 中。您需要重写代码以不使用属性 rest(...rest对象文字的一部分)(或者,正如CertainPerformance 建议的那样,使用转译器)。
Here's oneof many ways to do that:
这是执行此操作的众多方法之一:
function sortByCategory(data) {
return data.reduce((obj, c) => {
//const { category, ...rest } = c;
const { category } = c;
const rest = {};
for (const key of Object.keys(c)) {
if (key !== "category") {
rest[key] = c[key];
}
}
obj[category] = obj[category] || [];
obj[category].push(rest);
return obj;
}, {});
}
I avoided using deletebecause deleteon an object de-optimizes the object, making property lookups slower. But deoptimizing just these objects may well not make any difference to the perceived speed of your page/app, so...
我避免使用,delete因为delete在对象上取消优化对象,使属性查找变慢。但是仅对这些对象进行去优化可能不会对您的页面/应用程序的感知速度产生任何影响,因此...
回答by CertainPerformance
Neither Edge nor IE supportobject property rest syntax (though Edge will likely support it eventually). I'd suggest automatically transpiling your code to ES5 with Babel, which will allow you to write in the latest and greatest version of the language, while allowing ancient and incompatible browsers to understand all of your transpiled code. For example, plugging in
Edge 和 IE 都不支持对象属性剩余语法(尽管 Edge 最终可能会支持它)。我建议使用Babel自动将您的代码转换为 ES5 ,这将允许您使用该语言的最新和最好的版本进行编写,同时允许古老且不兼容的浏览器理解您所有的转换代码。例如,插入
const { category, ...rest } = c;
results in
结果是
"use strict";
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
var _c = c,
category = _c.category,
rest = _objectWithoutProperties(_c, ["category"]);
Doesn't look so pretty, but it can be done automatically.
看起来不那么漂亮,但它可以自动完成。
One manual way of doing it could be:
一种手动方法可能是:
const c = {
category: 'category',
foo: 'foo',
bar: 'bar'
};
const category = c.category;
// Object.assign so as not to mutate the original object:
const rest = Object.assign({}, c);
delete rest.category;
console.log(rest);
回答by Remi Patriarche
Edge 44 fails with "SCRIPT1028: Expected identifier, string or number" with :
Edge 44 失败并显示“SCRIPT1028:预期标识符、字符串或数字”,并显示:
var a = {};
a = { ...a};
But succeeded with:
但成功了:
var a = {};
a = Object.assign({}, a);
回答by Ogglas
As of 2020-05-12 this will work in Microsoft Edge. Tested version: Microsoft Edge Version 81.0.416.72 (Official build) (64-bit)
从 2020 年 5 月 12 日起,这将在 Microsoft Edge 中运行。测试版本:Microsoft Edge Version 81.0.416.72 (Official build) (64-bit)
https://www.microsoft.com/en-us/edge
https://www.microsoft.com/en-us/edge
The following lines of code caused the exception SCRIPT1028: Expected identifier, string or numberbut when updating everything worked:
以下代码行导致异常,SCRIPT1028: Expected identifier, string or number但更新时一切正常:
return {
...state,
products: [...state.products, ...action.payload],
};

