更新时间:2023-02-23 21:40:31
隐藏"变量名称通常是不好的做法.如果您不小心,可能会导致对所引用内容的混淆.
It's generally bad practice to "shadow" variable names. It can cause confusion about what's being referenced if you aren't careful.
在此示例中,没有主要缺点.考虑一下,如果以后你决定使函数递归.如果你试图从它自身内部调用 sum
,你会得到一个错误,指出 sum
不是一个函数,因为它正在寻找内部变量 sum代码>,而不是函数.这不是主要问题,但编写将来不太可能以奇怪方式破坏的代码是个好主意.你永远不知道你以后可能会做出什么改变.
In this example, there isn't a major downside. Consider though if later you decided to make the function recursive. If you tried to call sum
from within itself, you'd get an error that sum
isn't a function, because it's finding the inner variable sum
, not the function. That's not a major issue, but it's a good idea to write code that is less likely to break in weird ways in the future. You never know what changes you might make later on.