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

javascript - Spread operator ES5 equivalent - Stack Overflow

programmeradmin2浏览0评论

Below is the code I am trying to convert to ES5 for IE patibility.

$.when(...requests).then(function(...responses) {
   // More code goes here
}

So far I could convert it to the below and it works well, but I still couldn't replace the then part.

$.when.apply(null, requests).then(function(...responses) {
   // More code goes here
}

Any suggestion would be really appreciated.

Below is the code I am trying to convert to ES5 for IE patibility.

$.when(...requests).then(function(...responses) {
   // More code goes here
}

So far I could convert it to the below and it works well, but I still couldn't replace the then part.

$.when.apply(null, requests).then(function(...responses) {
   // More code goes here
}

Any suggestion would be really appreciated.

Share Improve this question asked May 31, 2021 at 15:17 Parag KadamParag Kadam 3,8805 gold badges27 silver badges57 bronze badges 2
  • 1 Why not transpile the code? – VLAZ Commented May 31, 2021 at 15:28
  • 1 Also, you seem to only be asking about rest parameter syntax: function(...responses). Only $.when(...requests) is using spread. – VLAZ Commented May 31, 2021 at 15:30
Add a ment  | 

1 Answer 1

Reset to default 4

There is arguments:

$.when.apply(null, requests).then(function() {
   var responses = [].concat.apply([], arguments); // Get a standard array from arguments object
   // More code goes here
}

As a disclaimer I quote MDN on the use of arguments:

If you're writing ES6 patible code, then rest parameters should be preferred.

So when you don't have ES6 support (which seems to be your case), then using arguments is fine, but otherwise you should use the spread syntax in the function parameter list.

发布评论

评论列表(0)

  1. 暂无评论