Is it possible to tell a function what names it should give to its arguments, based on the number of arguments passed? Like this:
function soSwag(swagLevel, swagStart, swagDuration || swagStart, swagStop){}
Is it possible to tell a function what names it should give to its arguments, based on the number of arguments passed? Like this:
function soSwag(swagLevel, swagStart, swagDuration || swagStart, swagStop){}
Not to my knowledge. However, you can just pass in an object with the data you want.
soSwag({
swagStart: 10,
swagStop: 100
});
However, it might be cleaner to simply pass null
into that first parameter.
What some popular libraries (e.g. jQuery) that support variable types of arguments do is that they examine the arguments and then swap values accordingly:
function soSwag(swagLevel, swagStart, swagDuration) {
if (arguments.length === 2) {
var swagStop = swagStart; // 2nd arg becomes swagStop
swagStart = swagLevel; // first arg becomes swagStart
// code here that uses swagStart and swagStop
// as called like soSwag(swagStart, swagStop)
} else {
// code here that uses swagLevel, swagStart, swagDuration
// as called like soSwag(swagLevel, swagStart, swagDuration)
}
}