javascript “变量初始化器是多余的”究竟是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32123705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What exactly does "variable initializer is redundant" mean?
提问by dbinott
So I am using Webstorm IDE for some JavaScript and I believe it uses JSLint to inspect the code. I have a bunch of variable initializer is redundant
warnings. I can't find anything about what it exactly means in terms of what I need to fix.
所以我将 Webstorm IDE 用于一些 JavaScript,我相信它使用 JSLint 来检查代码。我有一堆variable initializer is redundant
警告。我找不到任何关于它在我需要修复的方面的确切含义的信息。
Example:
例子:
function calcPayNow(){
var payNowVal = 0;
--snip---
payNowVal = (Math.round(tec * x) - Math.round(allpmts * x)) / x;
--snip--
}
回答by dsh
It means that there is no purpose to assigning 0 because it is never used before you assign a different value.
这意味着分配 0 没有任何意义,因为在您分配不同的值之前它永远不会被使用。
I would change it from:
我会把它从:
var payNowVal = 0;
--snip---
payNowVal = (Math.round(tec * x) - Math.round(allpmts * x)) / x;
to
到
--snip---
var payNowVal = (Math.round(tec * x) - Math.round(allpmts * x)) / x;
回答by Halcyon
You're assigning the value 0
, but that value is never used. It's discarded immediately when you reassign it.
您正在分配 value 0
,但从未使用过该值。当您重新分配它时,它会立即被丢弃。
Change to:
改成:
function calcPayNow(){
var payNowVal;
--snip---
payNowVal = (Math.round(tec * x) - Math.round(allpmts * x)) / x;
--snip--
}
回答by Paul Roub
You're initializing payNowVal
here:
你在payNowVal
这里初始化:
var payNowVal = 0;
And presumably never usingthat value, then later setting it to:
并且大概从不使用该值,然后将其设置为:
payNowVal = (Math.round(tec * x) - Math.round(allpmts * x)) / x;
So the initialization does nothing for you. It's redundant.
所以初始化对你没有任何作用。这是多余的。
Leave it off, or better yet, don't declare it until you have a value:
离开它,或者更好的是,在你有一个值之前不要声明它:
function calcPayNow(){
// --snip---
var payNowVal = (Math.round(tec * x) - Math.round(allpmts * x)) / x;
//--snip--
}
回答by Bergi
It means that you are unnecessarily initialising your variable. Omit that = 0
, you're never needing (using) it. Or just do
这意味着您不必要地初始化您的变量。省略那个= 0
,你永远不需要(使用)它。或者只是做
var payNowVal = (Math.round(tec * x) - Math.round(allpmts * x)) / x;