Andrei Karpuszonak
Andrei Karpuszonak

Reputation: 9044

Clarity in providing multiple parameters to the function in Javascript

I would like to provide multiple parameters to the Javascript function, and also having clarity regarding passed parameters, so having this function

var callMe = function (param1, param2, param3) {
  console.log(param1 + ", " + param2 + ", " + param3) 
}

I would call the method callMe() like this, having in mind that value of parameter initialization is the value itself:

callMe(param1 = 1, param2 = 2, param3 = 3)

My goal is to have clarity when providing multiple parameters to avoid confusion.

Are there any negative effects in the example above, or may be I'm just trying to reinvent the wheel?

Upvotes: 1

Views: 179

Answers (2)

MACMAN
MACMAN

Reputation: 1971

Use this:

var callMe = {
              param1:1,
              param2:2,
              param3:3
};

Upvotes: -1

Stephen
Stephen

Reputation: 5470

If you're not declaring your params ahead of time with a var, you're introducing global variables like crazy. And I can imagine that it'd get very, very sloppy very quickly. Instead of doing that, use a configuration object as the argument. Though it has its own drawbacks, you are essentially doing the same thing while wrapping the args in a way that it doesn't (as you say) reinvent the wheel. So..

var f = function (config) {
    console.log(config.param1 + ", " + config.param2 + ", " + config.param3);
}

f({
    param1: "Foo",
    param2: "Bar",
    param3: "Baz"
});

Upvotes: 2

Related Questions