2

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?

jt.
  • 7,625
  • 4
  • 27
  • 24

2 Answers2

8

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

https://caniuse.com/#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.org/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.com/#feat=mdn-javascript_builtins_object_assign

Alex L
  • 4,168
  • 1
  • 9
  • 24
  • Alex L, my version is `Microsoft Edge 44.18362.449.0` `Microsoft EdgeHTML 18.18363`, default instaled on sistem – Vladyslav Levenets Mar 12 '20 at 14:23
  • Perfect, then you can use ``Object.assign()`` no problem, it is supported since Edge 12: https://caniuse.com/#feat=mdn-javascript_builtins_object_assign – Alex L Mar 12 '20 at 14:50
1

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.com/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>
Yu Zhou
  • 11,532
  • 1
  • 8
  • 22