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

ember.js - javascript spread operator for object is not working? Is this expected behavior? - Stack Overflow

programmeradmin5浏览0评论

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.

.js#L203

Am I missing any configuration to make it work ?.

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./josemarluedke/ember-cli-daterangepicker/blob/master/addon/ponents/date-range-picker.js#L203

Am I missing any configuration to make it work ?.

Share Improve this question asked Apr 4, 2017 at 13:47 Ember FreakEmber Freak 12.9k5 gold badges26 silver badges55 bronze badges 4
  • 1 Yes, this is expected. There is no spread syntax for objects in ES6. – Bergi Commented Apr 4, 2017 at 13:50
  • 4 github./josemarluedke/ember-cli-daterangepicker/issues/66 – Bergi Commented Apr 4, 2017 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 Commented Apr 4, 2017 at 14:08
  • 1 ... is not an operator. – Felix Kling Commented Apr 4, 2017 at 19:08
Add a ment  | 

2 Answers 2

Reset to default 4

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/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator

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]));

发布评论

评论列表(0)

  1. 暂无评论