Reasoning
Currently if you are using a callback function but not using the first, second, etc. argument it's a little awkward. Which currently looks like:
object.method((_firstUnusedParam, _secondUnusedParam, thirdParam) => thirdParam)
// or
object.method((_, __, thirdParam) => thirdParam)
A couple real world examples
// Array.prototype.map
array.map((_value, index) => `item: ${index + 1}`)
// express
app.get('/', (_req, res) => {
res.send('ok')
})
Idea
Similar to array destructuring
const [, , thirdValue] = array
It would be convenient if you could omit preceding positional function arguments if you did not need them like this:
object.method((, , thirdParam) => thirdParam)
Real world examples:
array.map((, index) => `item: ${index + 1}`)
// express
app.get('/', (, res) => {
res.send('ok')
})
3 Likes
While this would be fewer characters for the author to type it might not help when reading the code later. The convention of using the single _ or a prefixed _ feels easier to follow when quickly glancing through code for me personally.
I am not so much worried about the typing, so much as it feels cumbersome declaring variables I'm not going to use. I think
(_, _, param) => param
would be just as good, but unfortunately you cannot reuse the underscore. But perhaps you are right that just the _ with the variable name is not so bad.
You don't have the same intuition about the array destructing though, or would you prefer people also do?
const [_value1, _value2, value3] = array
So it has. Thanks for the info. One of the commit members point out you could co-opt the array syntax for the same purpose.
app.get('/', (...[, res]) => {
res.send('ok')
})
array.map((...[, index]) => `item: ${index + 1}`)
Also a little awkward, but maybe useful if you needed to skip two or more parameters, otherwise, for one I'd probably just use the underscore _
.
app.get('/', (_req, _res, next) => {
console.log('root request')
next()
})
// vs.
app.get('/', (...[, , next]) => {
console.log('root request')
next()
})
1 Like
Huh, I kind of liked this idea of omitting function parameters. I can certainly see it being annoying to figure out what to do with optional parameters - Using an underscore works ok for a single parameter, but things get awkward when there's multiple. I've certainly run into this problem a small handful of times.
1 Like
Here I come again with another suggestion: What if there were a notation on the function declaration that allowed for optional positioning of those parameter values from the caller? Something like this:
function example(param1, ?param2, ?param3, param4) {
//Do whatever
}
This would work out just the same as if the following had been written instead:
function example(param1, param2, param3, param4) {
switch (arguments.length) {
case 2:
param4 = param2;
param2 = void 0;
break;
case 3:
param4 = param3;
param3 = void 0;
break;
}
//Do whatever
}
The general idea is to ensure that all non-optional parameters are filled first, then fill optional parameters in order given additional available parameter data.