简体   繁体   中英

Spread operator ES5 equivalent

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

$.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.

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 compatible 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM