18

I started using object spread syntax to safely make a copy of the object while following immutability principles.

I want to use it in chrome developer console - how to do this:

enter image description here

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
Probosckie
  • 1,585
  • 4
  • 25
  • 40
  • 2
    `Object.assign()` will do a shallow copy of an object. – jfriend00 Feb 06 '17 at 06:21
  • 4
    [`...` is not an operator](http://stackoverflow.com/questions/37151966/what-is-spreadelement-in-ecmascript-documentation-is-it-the-same-as-spread-oper/37152508#37152508). – Felix Kling Feb 06 '17 at 06:24
  • 5
    Object spread is a [*proposal*](https://github.com/tc39/proposals) and is not available in the current stable version of Chrome. – Felix Kling Feb 06 '17 at 06:25

3 Answers3

19

You can enable the javascript experiments flag on: chrome://flags/#enable-javascript-harmony to enable web pages to use experimental JavaScript features including the Rest / Spread.

Washington Braga
  • 1,593
  • 15
  • 14
  • Im starting to learn es7- Will I be able to use es7 features in chrome dev console if I turn on this flag?? – Probosckie Jun 22 '17 at 06:21
5

Object spread is available in all modern browsers (since Chrome 60) and can be used like this:

{...{foo: 'bar'}, john: 'doe'}

And will return: {foo: "bar", john: "doe"}

sqren
  • 22,833
  • 7
  • 52
  • 36
  • @Aequitas Works for me. Try this: `{...{foo: 'bar'}}` – sqren Oct 30 '18 at 11:08
  • Ah my bad, it's the array spread that doesn't work (sometimes): `let a = ...['foo']`, fails with unexpected token. But `console.log(...['foo'])` will work just fine – Aequitas Oct 30 '18 at 22:34
  • 1
    @Aequitas You must spread into another array: `[...['foo'], 'bar']` – sqren Oct 30 '18 at 22:39
-2

... is not an operator (at least not in the sense the ECMAScript spec uses the term "operator"). It is actually called "spread syntax", or "rest syntax".

MauroPorras
  • 5,079
  • 5
  • 30
  • 41
Robsonsjre
  • 1,586
  • 11
  • 17
  • 1
    [compat-table](http://kangax.github.io/compat-table/esnext/#test-object_spread_properties) says that Chrome 58 has support for object spread, but its hidden by flag – Vladimir Starkov Feb 07 '17 at 11:43
  • 1
    chrome://version is revealing that the current official build for windows is 55.something; might have to install canary to try this – Probosckie Feb 07 '17 at 18:29
  • 5
    @Robsonsjre Please make an effort to be more friendly. If OP mentions "..." as an operator, don't just say that it isn't one. You could have said that it's actually called "spread syntax" or "rest syntax", provide some refs/articles. On the other hand, more of the same half information. It's not in stable Chrome but there are flags to enable it. – Neithan Max May 17 '17 at 07:40