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

javascript - Spread operator alternative - Stack Overflow

programmeradmin2浏览0评论

I have the following code I received:

function sortByProp(...props) {
  const callback = props.pop();
  return function(a, b) {
    const v1 = retrieve(props, a);
    const v2 = retrieve(props, b);
    return callback(v1, v2);
  } 
}

It works great in most browsers, but not in opera. I get the following error:

function sortByProp(...props) {
                    ^^^
SyntaxError: Unexpected token ...

I visited to see if there is an alternative to see if there is a polyfill for this, but I can't find one.

I call this function using these two:

.sort(sortByProp('key', 'k', a))
          .sort(sortByProp('key', 'n', b));

How can I make it so this will work in all browsers?

I have the following code I received:

function sortByProp(...props) {
  const callback = props.pop();
  return function(a, b) {
    const v1 = retrieve(props, a);
    const v2 = retrieve(props, b);
    return callback(v1, v2);
  } 
}

It works great in most browsers, but not in opera. I get the following error:

function sortByProp(...props) {
                    ^^^
SyntaxError: Unexpected token ...

I visited https://developer.mozilla/en/docs/Web/JavaScript/Reference/Operators/Spread_operator to see if there is an alternative to see if there is a polyfill for this, but I can't find one.

I call this function using these two:

.sort(sortByProp('key', 'k', a))
          .sort(sortByProp('key', 'n', b));

How can I make it so this will work in all browsers?

Share Improve this question asked May 23, 2016 at 21:13 user2924127user2924127 6,24218 gold badges85 silver badges146 bronze badges 1
  • Remove ...props, add var props = arguments;? Actually, may need to convert to an array, so [].slice.call(arguments); – Niet the Dark Absol Commented May 23, 2016 at 21:15
Add a ment  | 

1 Answer 1

Reset to default 6

You can't polyfill syntax. It just doesn't work. Syntax checking happens before any logic from a polyfill can occur. What you can do is use pilers like Babel to convert your ES2015 code down to ES5. Or you can rewrite your function to use arguments:

function sortByProp() {
  var props = [].slice.call(arguments);
  const callback = props.pop();
  return function(a, b) {
    const v1 = retrieve(props, a);
    const v2 = retrieve(props, b);
    return callback(v1, v2);
  } 
}
发布评论

评论列表(0)

  1. 暂无评论