2

Spread operator(...) for an object is not working as you can see in the below code. Is this expected behavior? try it in jsbin

//var test ={start:'stating',end: 'ending'}; //its not working
var test=['start-value','end-value']; //its working
function display(start,end){
  console.log(start,end);
}
display(...test)

The reason behind asking this question is, I saw the use of spread operator using an object in the below line of code but it's not working for me.

https://github.com/josemarluedke/ember-cli-daterangepicker/blob/master/addon/components/date-range-picker.js#L203

Am I missing any configuration to make it work ?.

Ember Freak
  • 12,918
  • 4
  • 24
  • 54
  • 1
    Yes, this is expected. There is no spread syntax for objects in ES6. – Bergi Apr 04 '17 at 13:50
  • 4
    https://github.com/josemarluedke/ember-cli-daterangepicker/issues/66 – Bergi Apr 04 '17 at 13:52
  • @Bergi thanks for the issue tracker link. Actually, that was ember-addon so I thought they might do some kind of transpilation while preparing final output js to make it work. but that's not the case for this issue so I can go ahead and write my very first PR for Open Source. – Ember Freak Apr 04 '17 at 14:08
  • 1
    [`...` is not an operator](http://stackoverflow.com/a/37152508/218196). – Felix Kling Apr 04 '17 at 19:08

2 Answers2

5

From the docs:

Only for iterables

Note that the spread operator can be applied only to iterable objects:

var obj = {'key1': 'value1'};
var array = [...obj]; // TypeError: obj is not iterable

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator

Cruiser
  • 1,618
  • 2
  • 16
  • 20
2

I guess you are looking forward to get the right values from the object as your function arguments. You are able to use the spread operator, but firstly you have to exchange the object into something iterable, e.g. array.

var test = {
  start: 'stating',
  end: 'ending'
}

function display(start, end) {
  console.log(start, end);
}
display(...Object.keys(test).map(v => test[v]));
kind user
  • 40,029
  • 7
  • 67
  • 77