简体   繁体   中英

It is considered bad practice to assign a value to an implicitly declared local variable of a function?

Question: It is considered bad practice to assign a value to an implicitly declared local variable of a function? if so, what is the perfered method of assigning a implicitly declared local variable of a function

function foo(baz) {
  baz = 3;  //this is implicitly declared by the function's argument baz


}
foo()

I wouldn't call it a bad practice (as in harmful), just not a good one. A beneficial effect is to reduce code length by 4 characters as it replaces a variable declaration within the function. That's only for the first variable, after that there is no benefit and if other variables are declared, there is zero benefit.

However, code readability suffers and the function signature is needlessly obfuscated, eg

function foo(bar) {
  /* some code */
}

leaves the user wondering what should be passed to bar, whereas:

function foo() {
  var bar;
  /* some code */
}

makes it clear that foo doesn't take any arguments.

Formal parameters are processed before declarations, but I can't see any benefit or side effects from that.

I think it's a bit useless. Because it doesn't matter what value you pass, you just overwrite it with whatever you want. So, not a bad practice, just a useless function. No offense please.

That reminds me:

function random_number() {
    return 7;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM