admin管理员组

文章数量:1315025

Is the following function legal and portable?

function(_, _, x){
    return x;
}

Sometimes I want to write a callback that doesn't use the leftmost parameters so I wonder what is the most concise way to do so.


Conclusion:

function(_1, _2, x) is probably as short as it gets then.

Is the following function legal and portable?

function(_, _, x){
    return x;
}

Sometimes I want to write a callback that doesn't use the leftmost parameters so I wonder what is the most concise way to do so.


Conclusion:

function(_1, _2, x) is probably as short as it gets then.

Share Improve this question edited Jul 28, 2011 at 1:44 hugomg asked Jul 27, 2011 at 23:21 hugomghugomg 70k29 gold badges164 silver badges255 bronze badges 5
  • If your callback sometimes doesn't use the leftmost arguments, but always uses the rightmost one, why not promote that argument to first position and omit the other two in your calls? – Frédéric Hamidi Commented Jul 27, 2011 at 23:24
  • 1 If you don't use those parameters, then why have them? – Matt R. Wilson Commented Jul 27, 2011 at 23:24
  • 1 @Frédéric I'm guessing they're using some kind of API where you provide a callback function. – Phil Commented Jul 27, 2011 at 23:26
  • 4 I can't change the signature of the callback because I didn't write the code that invokes it. – hugomg Commented Jul 27, 2011 at 23:26
  • +1 For a good question, and I was interested to see the answers. But although I can understand why you'd want to do this I'd be more inclined to go with the less concise but more obvious function(notUsed1,notUsed2,x) - or name the parameters for what they actually are and simply not use them. More readable, and you don't have to worry about it breaking in some obscure browser (or in strict mode of the popular browsers as per Šime's answer). – nnnnnn Commented Jul 27, 2011 at 23:44
Add a ment  | 

5 Answers 5

Reset to default 9

It is valid in non-strict mode code, but invalid in strict mode code:

It is a SyntaxError if any Identifier value occurs more than once within a FormalParameterList of a strict mode FunctionDeclaration or FunctionExpression.

Source: http://es5.github./#x13.1

Therefore, you may want to avoid this, since at one point in the future you will want to move on to strict mode...

You can use arguments, but yes that will work:

function test(_, _, x){
    console.log(arguments);
    return x;
}

console.log(test('a','b','c'));

Outputs:

["a", "b", "c"]
c

http://jsfiddle/JdrDY/

And here is what it prints if you try to use the _ argument:

["a", "b", "c"]
b
c

http://jsfiddle/JdrDY/2/

var ignoreLeftParam = function(count, f) {
    return function() {
        f.apply(this, Array.prototype.slice.call(arguments, count));
    }
}

ignoreLeftParam(2, function(x) {
    return x;
});

Writing a general utility to ignore parameters might be considered neater.

If you're running into problems with this kind of function signature, try rewriting it to accept a single options object with named properties. That way you can pass whatever bination of arguments you want.

E.g.:

function test(options) {
   if('numberOfHands' in options)
      console.log('I have ' + options.numberOfHands + ' hands!');

   if('duration' in options)
      console.log('I last for ' + options.duration);
}

Took me all of five seconds to test and apparently yes, this is legal.

Off topic

I can see why you may want to do this. Take jQuery's $.get() method for example. Say your "success" callback function only had to use the third jqXHR argument.

$.get('url', function(_, _, jqXHR) {
    // only use jqXHR
});

Though you may as well name the first two arguments x and y.

本文标签: Am I allowed to repeat function parameter names in JavascriptStack Overflow