最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Spread operator alternative - Stack Overflow

matteradmin5PV0评论

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);
  } 
}
Post a comment

comment list (0)

  1. No comments so far