Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
520 views
in Technique[技术] by (71.8m points)

javascript - Edge: SCRIPT1028: Expected identifier, string or number

My page works fine in Chrome and Firefox: enter image description here

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 gives me the following error:

SCRIPT1028: SCRIPT1028: Expected identifier, string or number on line 84 of faq.html

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 ...rest part of your object literal) (or, as CertainPerformance suggests, use a transpiler).

Here's one of 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 delete because delete on 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...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...