I am looking for a way to declare a function without declaring one or more parameters. This would be used in cases where you're implementing a function where you don't care what the value of (for instance) the first argument is.
For example, given this function:
function foo(this_arg_is_unused, important_arg) {
doSomethingWith(important_arg);
}
Is there some way to declare it more like this?
function foo( , important_arg) {
doSomethingWith(important_arg);
}
I realize I could easily do this:
function foo() {
doSomethingWith(arguments[1]);
}
However that starts becoming less readable and it would be more difficult to (for instance) use fn.js to curry the arguments.
I'm currently thinking perhaps I could just use a visual mnemonic to indicate the argument is not used:
function foo(ø, important_arg) {
doSomethingWith(important_arg);
}
However this may also have readability issues.
Real world example:
The callback function used by jQuery's .each() takes two arguments, the first is either the index or property name in the array/object and the second is the value. To manipulate some div
elements you might do something like this:
$($('div').each(function(index, value) {
console.log(value); // equivalent to doSomething(this)
}));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>Nothing here</div>
Using an arrow function (where using arguments[] is not possible)
$($('div').each((ø, value) => console.log(value)));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>Nothing here</div>