最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Object spread operator throw error in microsoft edge - Stack Overflow

programmeradmin4浏览0评论

I have code:

let a = {a: 'a', b: 'b'};
let b = {c: 'c', d: 'd'};
let c = {...a, ...b};

In chrome/firefox/... its display: c = {a: 'a', b: 'b', c: 'c', d: 'd'}, but in microsoft edge it throw error Expected identifier, string or number.

I try to use cdn.polyfill.io and but no luck.

What i can do to run my webpack code in microsoft edge?

I have code:

let a = {a: 'a', b: 'b'};
let b = {c: 'c', d: 'd'};
let c = {...a, ...b};

In chrome/firefox/... its display: c = {a: 'a', b: 'b', c: 'c', d: 'd'}, but in microsoft edge it throw error Expected identifier, string or number.

I try to use cdn.polyfill.io and https://babeljs.io/docs/en/babel-polyfill but no luck.

What i can do to run my webpack code in microsoft edge?

Share Improve this question edited Sep 11, 2020 at 14:44 jt. 7,7054 gold badges28 silver badges24 bronze badges asked Mar 12, 2020 at 9:51 VladVlad 1001 silver badge13 bronze badges 3
  • Are you using babel ? If yes, can your post your babel config ? – Alexandre Nicolas Commented Mar 12, 2020 at 9:53
  • Please check the patibility version from here developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – brk Commented Mar 12, 2020 at 9:54
  • Kornflexx, i dont use babel to pile my code but i find some post that you can include babel polifyls as script, and because of that i try to use that. – Vlad Commented Mar 12, 2020 at 14:09
Add a ment  | 

2 Answers 2

Reset to default 8

It should be available in Edge since 79 without any transpiler (like Babel) needed (but not IE, don't confuse them).

https://caniuse./#feat=mdn-javascript_operators_spread_spread_in_object_literals

That said you could in most situations just use Object.assign() instead if you want -

https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

Your code would then be:

let a = {a: 'a', b: 'b'};
let b = {c: 'c', d: 'd'};
let c = Object.assign(a,b)

console.log(c)

Object.assign() is supported since Edge 12:

https://caniuse./#feat=mdn-javascript_builtins_object_assign

The { ...obj } syntax is called "Object Rest/Spread Properties" and it's a part of ECMAScript 2018 which is not supported by Edge Legacy. You can use Babel to transpile it.

If you just want to use it in non Node.js environments, you can use babel-standalone. You just need to load the babel-standalone in your script and write the script you want to transpile in script tag with type “text/babel” or “text/jsx”, the result in Edge Legacy will be {"a":"a","b":"b","c":"c","d":"d"}:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="https://cdnjs.cloudflare./ajax/libs/babel-standalone/6.18.1/babel.min.js"></script>
</head>
<body>
    <script type="text/babel">
        let a = { a: 'a', b: 'b' };
        let b = { c: 'c', d: 'd' };
        let c = { ...a, ...b };
        console.log(JSON.stringify(c));
    </script>
</body>
</html>
发布评论

评论列表(0)

  1. 暂无评论