Skip to content Skip to sidebar Skip to footer

Object Spread Operator Throw Error 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 ed

Solution 1:

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

Solution 2:

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><metacharset="utf-8" /><title></title><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.18.1/babel.min.js"></script></head><body><scripttype="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>

Post a Comment for "Object Spread Operator Throw Error In Microsoft Edge"