This question is closely related to, but I believe not a duplicate of, Passing named arguments to a Javascript function [duplicate] and Named parameters in javascript.
Various responses and discussions on those posts suggest different approaches to handle the absence of built-in support for named arguments in JavaScript.
The main concern raised by the original poster was as follows:
When calling a JavaScript function like this:
someFunction(1, true, 'foo');
it might not be very clear without prior knowledge of the function.
Assuming that someFunction
is defined elsewhere in the code as:
function someFunction(numberOfClowns, wearingHat, screamingAtTopOfLungs) {
console.log(arguments)
}
Is there a specific reason why the function couldn't be called like this?
someFunction(numberOfClowns=1, wearingHat=true, screamingAtTopOfLungs='foo')
Based on initial testing, this method seems to work without causing any errors and effectively resolves clarity issues.
It is important to declare all variables with var
beforehand and to understand that variable assignment is taking place, so the unexpected change in numberOfClowns
to 1 should not come as a surprise. Anything else that should be taken into consideration?