Does Javascript/ES8 have shorthand argument names, like Swift?
eg. arr.sort { $0.name > $1.name }
Does Javascript/ES8 have shorthand argument names, like Swift?
eg. arr.sort { $0.name > $1.name }
There are no shorthand arguments in Javascript. The closest alternatives are arguments
and ES6 rest parameter.
function fn(param1, param2) {
const $ = arguments;
// by name
console.log(param1, param2);
// by order
console.log($[0], $[1]);
}
Rest parameter it is more consistent than arguments
, the latter isn't supported in arrow functions:
const fn = (...$) => {
// by name
let [param1, param2] = $;
console.log(param1, param2);
// by order
console.log($[0], $[1]);
}
Notice that $
parameter name is used to draw an analogy to Swift. It is not recommended to use $
name in practice because it may be confused with conventional jQuery $
variable. The conventional meaningful name for rest parameter that includes all arguments is args
.