JavaScript:错误 - “需要赋值或函数调用,而看到的是表达式”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4167113/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 10:34:30  来源:igfitidea点击:

JavaScript: Error - "Expected an assignment or function call and instead saw an expression"?

javascriptjqueryjslint

提问by HeatherK

I am using JSLintto ensure my JavaScript is "strict" and I'm getting the following error:

我正在使用JSLint来确保我的 JavaScript 是“严格的”,但出现以下错误:

Expected an assignment or function call and instead saw an expression

期望赋值或函数调用,而是看到了一个表达式

On the following code:

在以下代码中:

(my_var > 0 ) ? $("#abc").html(my_array.join('')) : $("#abc").html('<h2>Hello ' + persons_name);

Any ideas why I'm getting such an error? Also, I'm using jQuery as seen in the above code, in case that makes a difference.

任何想法为什么我会收到这样的错误?另外,我正在使用上面代码中看到的 jQuery,以防万一。

采纳答案by Mark Bessey

My guess would be that JSLint is unhappy since you're using the ternary operator, and you're not doing anything with the value. Refactoring this into the equivalent:

我的猜测是 JSLint 不高兴,因为您使用的是三元运算符,并且您没有对值做任何事情。将其重构为等效的:

if (my_var > 0 ) {
  $("#abc").html(my_array.join(''));
} else {
  $("#abc").html('<h2>Hello ' + persons_name);
}

would eliminate the error. If for some reason you're really attached to using the ternary operator, the "right" way to use it would be:

将消除错误。如果由于某种原因你真的喜欢使用三元运算符,那么使用它的“正确”方法是:

$("#abc").html((my_var > 0) ? my_array.join('') : '<h2>Hello ' + persons_name);

回答by Matt

I believe this is because the ternary operator evaluates the expression and returns a value that is expected to be assigned. For example:

我相信这是因为三元运算符计算表达式并返回一个预期分配的值。例如:

var test = (my_var > 0) ? true : false;

However, you are using it like a regular if/then/else statement. While the ternary operator does perform if/then/else, it is traditionally used in assignents.

但是,您可以像使用常规 if/then/else 语句一样使用它。虽然三元运算符确实执行 if/then/else,但它传统上用于赋值语句。

EDIT: As an addendum: would this statement make sense to you?

编辑:作为附录:这个声明对你有意义吗?

var foo = 1;
(my_var > 0) ? true : false;
console.log('hello world');

回答by Adrien Plisson

you are using an expression (an expression using the ternary operator to be precise) in a single line: your line is comprised uniquely of an expression.

您在一行中使用表达式(准确地说是使用三元运算符的表达式):您的行由唯一的表达式组成。

this is considered poor programming practice in many language, and could be rewritten using an ifstatement to render this line more clear.

在许多语言中,这被认为是糟糕的编程实践,可以使用if语句重写以使该行更清晰。

回答by Campbeln

Just asked the same Q without finding this one for some reason...

出于某种原因,只是问了同样的问题却没有找到这个问题......

The ternary returns a value that isn't being used so you're abusing the ternary structure by not using the value (even though the function calls are being made as intended).

三元返回一个未被使用的值,因此您通过不使用该值来滥用三元结构(即使函数调用是按预期进行的)。