8

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.

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? Commented Jul 27, 2011 at 23:24
  • 1
    If you don't use those parameters, then why have them? 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. 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. 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). Commented Jul 27, 2011 at 23:44

5 Answers 5

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.com/#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...

Sign up to request clarification or add additional context in comments.

4 Comments

"one point in the future". Incorrect. "avoid this, since you want to move onto strict mode today" Correct.
@Raynos I wouldn't go as far as to say that everyone should activate strict mode today.
Use strict mode today (as in actually use the features) and you'll likely wipe out half the browsers on the web. Strict mode is a real buzz-word at the moment, like all the HTML written as if it was XHTML. There are very few cases that actually need it (strict mode, that is - XML style markup is only needed if an XHTML DOCTYPE is used and the page is served as XML).
@RobG we are talking about ES5 strict mode. Not strict doctypes.
3

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.net/JdrDY/

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

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

http://jsfiddle.net/JdrDY/2/

5 Comments

I don't need to use arguments. The whole point is ignoring them :)
Did you test IE6-9, FF3.6,FF5,Chrome,Safari4, Opera?
@Raynos: no. I'm asking because I'm lazy :P
@Raynos - I do not have Safari, FF3, or IE 6, but all the rest work. If you have the three I mentioned and care to doublecheck and let me know, that would be great. :)
@missingno - He was talking to me, I presume.
1
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.

1 Comment

I'm not really going to use this because its not as concise as just doing, _1, etc, but I am a sucker for functional programing so I love to see someone also though this.
0

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 combination 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);
}

Comments

0

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.

2 Comments

It also took me 5 seconds to test on a single browser, but I still don't know if this is OK in the general case.
@missingno Fair enough. I too only checked quickly in Chrome. Works in IE7 too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.