2

This is related to, but in my mind not a duplicate of, Passing named arguments to a Javascript function [duplicate] and Named parameters in javascript.

Various answers and comments on those questions propose approaches to deal with the lack of JavaScript language support for named arguments.

The original poster's concern was this:

Calling a Javascript function with something like

someFunction(1, true, 'foo');

is not very clear without familiarity with the function.

Let's say someFunction is declared elsewhere in the code as:

function someFunction(numberOfClowns,wearingHat,screamingAtTopOfLungs) {
    console.log(arguments)
}

Is there any particular reason why you couldn't call the function like this?

someFunction(numberOfClowns=1, wearingHat=true,screamingAtTopOfLungs='foo')

From my preliminary testing, this seems to not result in any errors, and certainly addresses any issues of clarity.

I guess you would need to var all of the variables beforehand, and be aware that variable assignment is occurring, so not be too surprised that numberOfClowns is now equal to 1. Anything else that I'm not considering?

3 Answers 3

4

Since you're just using the assignments as labels anyway, why not simply use comments?

someFunction(/*numberOfClowns=*/1,  /*wearingHat=*/true, /*screamingAtTopOfLungs=*/'foo')

I've seen this done in C code (particularly for functions with 5 or more arguments), and it would avoid the nasty side-effects you mention.

Since there aren't actually any checks being done with the var-assignment version, this seems to have all the same benefits without the downsides.

Sign up to request clarification or add additional context in comments.

2 Comments

This was one of the recommended answers in another question. I think I was initially more enamored of my approach (which is more readable, I think) before I had remembered about var-scoping and assignment issues.
I'm upvoting, but I think @Bergi's answer is more clearly directed to the scope of my question, since my main concern was what problems I might face from my approach.
3

I guess you would need to var all of the variables beforehand, and be aware that variable assignment is occurring

This is a major problem in my eyes. It makes this a whole more complicated than any of the other approaches, and it's not a local solution - you're polluting your scope.

Anything else that I'm not considering?

The main argument of named parameters/named arguments is that you can order and omit them however you want, and the right values will still end up in the right variables. Your approach does not provide this. Better just use objects as everyone does.

I'd wager there will be more problems than without the assignments.

1 Comment

It's a good point that since a main argument for named arguments is due to argument order, maybe formatting it in this way might give someone else the impression that you could change the variable order without causing any problems.
1

As you say, assignments return the assigned value, so if the variables are properly declared, there is no problem. To avoid conflicts with other code in the same function, I suggest wrapping the call inside a block, and declaring the parameters with let.

function someFunction(numberOfClowns, wearingHat, screamingAtTopOfLungs) {
  console.log(numberOfClowns, wearingHat, screamingAtTopOfLungs);
}
{
  let numberOfClowns, wearingHat, screamingAtTopOfLungs;
  someFunction(numberOfClowns=1, wearingHat=true, screamingAtTopOfLungs='foo');
}

You may also be interested in destructuring objects:

function someFunction({numberOfClowns, wearingHat, screamingAtTopOfLungs}) {
  console.log(numberOfClowns, wearingHat, screamingAtTopOfLungs);
}
someFunction({numberOfClowns: 1, wearingHat: true, screamingAtTopOfLungs: 'foo'});

4 Comments

He already knows he can do it. He wanted to know if he's missed any reasons not to. (And he said he knows he needs to declare the local vars too.)
As a side note, destructuring is an es2015 feature, so run your code through Babel first if you want it to run on all browsers :)
@DaoWen OK, I skipped the last paragraph in the question. I have reworded the answer a bit.
Upvote for using let inside of a block to prevent stomping on scope.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.