3

Often, I define and call a function like this - particularly when we use flags:

function cat(doMeon, doCloseEyes)
{
  if (doMeon) console.log("Meon...");
  if (doCloseEyes) console.log("Hmm, I closed my eyes..."); 
}
...
// And then call, using flags. Names are temporary, only for readability
cat(doMeon=true, doCloseEyes=true);

Note that we could just do "cat(true, true)" but that kills the readability.

But the problem this introduces is that the two variables are now in global scope. And we shouldn't polluting the global scope. But we can't do this:

// Doesn't work...
cat(var doMeon=true, var doCloseEyes=true);

Is there an approach that one can use as best practice here? (Note: Defining these vars outside is not such a good idea.)

6
  • Where/how did you pick up that func(foo=true) style of invocation as a common practice? Are you trying to emulate another language? Commented Mar 28, 2013 at 3:28
  • You're describing is named parameters, which JavaScript doesn't have. Commented Mar 28, 2013 at 3:28
  • Javascript values are passed by position, not name. You can't do cat(var x) because it's a syntax error—the keyword var is a reserved word and can't be used where an identifier or expression is expected (var can only be used in a variable statement, which isn't an expression). Commented Mar 28, 2013 at 3:34
  • While Python can do this, MANY other language simply pass parameters by position. If you think this "kills the readability", maybe you could re-think of your coding and reading style. Commented Mar 28, 2013 at 3:40
  • I already said that it is wrong. We can't use "var" in params; perhaps javascript should allow that. And you can't just do "cat(true, true)" - it is so unreadable. What practice do you adopt in the above situation? Commented Mar 28, 2013 at 3:44

2 Answers 2

2

One method to pass in arguments with readability would be to pass an object containing each argument as a named property:

i.e.

cat({
    doMeon: true,
    doCloseEyes: true
});

And inside the function, you can do:

function cat(_props) {
    if (_props.doMeon) console.log("Meon...");
    if (_props.doCloseEyes) console.log("Hmm, I closed my eyes..."); 
    ....
}
Sign up to request clarification or add additional context in comments.

7 Comments

I am inclined to categorize this as 'Next best approach', because we end up creating an object. It just occurred to me - why not just use name as well as flag as arguments (and ignore the name part in the function), something like: "cat("doMeon", true, "doCloseEyes", true);"... After all, I am only looking for convention.
@uniwalker - Yes, but it would mean creating several objects instead of one (each string is an object). Not an issue if they are unnamed arguments, they'll be popped off the call stack and gc'd (sooner or later) once the function completes execution.
@techfoobar Local variables in javascript functions do not get gc'd even after the completion of the parent function execution but they will continue to reside in memory as long as they continue to exist in the closure of some function. But I do not know what happens to the "arguments" object. Are you sure it gets gc'd with function completion ?
@AksharPrabhuDesai - The argument values do get popped of the stack (like in all other langs as well) with the function once execution is done. The arguments variable will probably have some value as long as its not in the outermost scope.
@techfoobar No. It turns out that the argument objects too are treated like normal variables and continue to remain available as long as there are have any scope chain links. Example: jsfiddle.net/2Kv3y . I thought you were suggesting otherwise.
|
0

You could create two functions instead:

function catDoMeon()
{
  console.log("Meon...");
}

function catDoCloseEyes()
{
  console.log("Hmm, I closed my eyes..."); 
}

catDoMeon();
catDoCloseEyes();

2 Comments

Agree, but looking for a standard practice independent of application semantics.
@uniwalker This is a good practice actually; each of those flags is tied to a branch in your code, so it's better to separate them.

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.