JavaScript:预期和赋值或函数调用,而是看到一个表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23183972/
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
JavaScript : Expected and assignment or function call and instead saw an expression
提问by HSG
I am using JSHint to ensure my JavaScript is "strict" and I'm getting the following error:
我正在使用 JSHint 来确保我的 JavaScript 是“严格的”,但出现以下错误:
Expected an assignment or function call and instead saw an expression
期望赋值或函数调用,而是看到了一个表达式
On the following code:
在以下代码中:
var str = 'A=B|C=D'
var data = {};
var strArr = str.split( '|' );
for (var i = 0; i < strArr.length; i++) {
var a = strArr[i].split('=');
a[1] && (data[a[0].toLowerCase()] = a[1]); // Warning from JSHint
}
Any ideas why I'm getting such an error or how I can code to remove the error.
任何想法为什么我会收到这样的错误或我如何编写代码来消除错误。
回答by kapex
Here is a simplified version that gives the same warning:
这是一个提供相同警告的简化版本:
var a, b;
a && (b = a);
Expected an assignment or function call and instead saw an expression
期望赋值或函数调用,而是看到了一个表达式
This means that you have an expression but do not assign the result to any variable. jshint doesn't care about what the actual expression is or that there are side effects. Even though you assign something inside of the expression, you are still ignoring the result of the expression.
这意味着您有一个表达式,但没有将结果分配给任何变量。jshint 不关心实际表达式是什么或者有副作用。即使您在表达式内部分配了一些东西,您仍然忽略了表达式的结果。
There is another error by jslint if you care about it:
如果您关心它,jslint 还有另一个错误:
Unexpected assignment expression
意外的赋值表达式
This warns you that you may want to use ==
instead of =
inside logical expressions. It's a common error, therefore you are discouraged to use assignments in logical expressions (even though it is exactly what you want here).
这会警告您可能希望使用==
而不是=
内部逻辑表达式。这是一个常见的错误,因此不鼓励您在逻辑表达式中使用赋值(即使这正是您在这里想要的)。
Basically, jshint/jslint do not like misuse of shortcut evaluation of logical operator as replacement for if statements. It assumes that if the result of an expression is not used, it probably shouldn't be an expression.
基本上,jshint/jslint 不喜欢滥用逻辑运算符的快捷评估来代替 if 语句。它假设如果不使用表达式的结果,它可能不应该是一个表达式。
回答by Jigar
http://jshint.com/docs/options/#expr- JSHINT says, Expr warnings are part of relaxing options. So, if you write /* jshint expr: true */
, it won't give you the warning. But, you have to know the scope of function too. If you just type this line on top of everything, it will apply this rule globally. So, even if you made a mistake on the other lines, jshint will ignore it. So, make sure you use this wisely. Try to use if for particular function ( i mean inside one function only)
http://jshint.com/docs/options/#expr- JSHINT 说,Expr 警告是放松选项的一部分。所以,如果你写/* jshint expr: true */
,它不会给你警告。但是,您也必须知道功能范围。如果您只是在所有内容的顶部键入此行,它将全局应用此规则。因此,即使您在其他行上犯了错误,jshint 也会忽略它。因此,请确保您明智地使用它。尝试将 if 用于特定功能(我的意思是仅在一个功能内)