为什么我们不能在三元运算符中返回?

时间:2023-01-01 22:24:15

Say I've a simple form and I want to check whether form has changed or not. If its changed submit it else prevent form submission, so I used return and instead of using if-else statement I tried to use ternary operation but unfortunately I was hit with error Uncaught SyntaxError: Unexpected token return but I did not understand why this error? Is ternary operation only used to assign? Not sure on this part. Below is just a sample of what I was trying to do.

说我有一个简单的表格,我想检查表格是否已经改变。如果它改变提交它否则阻止表单提交,所以我使用return而不是使用if-else语句我试图使用三元操作但不幸的是我遇到了错误Uncaught SyntaxError:意外的令牌返回但是我不明白为什么会出现这个错误?三元运算只用于分配吗?在这方面不确定。以下只是我尝试做的一个示例。

var form_original_data = $("#frmProfile").serialize();

$("#frmProfile").on('submit', function(e) {
  e.preventDefault();
  $("#frmProfile").serialize() != form_original_data ? $("body").append('changed') : return;
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="frmProfile">
  <input type="text" value="name" />
  <input type="submit" value="Go" />
</form>

4 个解决方案

#1


25  

The ternary operator evaluates to an expression and expressions can't contain a return statement (how would that behave if you were to assign the expression to a variable?). However, you could very well return the result of a ternary operator, i.e. return condition? returnValue1 : returnValue2;

三元运算符求值为表达式,表达式不能包含return语句(如果要将表达式赋值给变量,那将会如何表现?)。但是,您可以很好地返回三元运算符的结果,即返回条件? returnValue1:returnValue2;

On your specific point, I don't see why you would like to return. It looks like you're trying to do something only if a condition is fulfilled. A simple if statement would probably be more adequate there.

在你的具体观点,我不明白你为什么要回来。看起来你只是在条件得到满足的情况下才会做某事。一个简单的if语句可能更适合那里。

#2


7  

JavaScript (like many other languages) has expressions and statements. An expression must evaluate to something. A statement performs an action. Expressions can be used as statements, but the reverse is not true.

JavaScript(像许多其他语言一样)具有表达式和语句。表达式必须评估某事。声明执行操作。表达式可以用作语句,但反之则不然。

return is a statement. It does not evaluate to anything. You are using a ternary expression (a.k.a ternary operator), which has the syntax

return是一个声明。它没有评估任何东西。您正在使用具有语法的三元表达式(a.k.a三元运算符)

test ? expression1 : expression2

and evaluates to expression1 if the condition holds, and expression2 otherwise. This implies that expression1 and expression2 must themselves evaluate to something, and cannot be statements.

如果条件成立则计算表达式1,否则计算表达式2。这意味着expression1和expression2必须自己评估某些东西,而不能是语句。


Bottom line, you should use an if.

最重要的是,你应该使用if。

#3


4  

Because it is not syntactically correct. Here's the correct syntax for a ternary operator -

因为它在语法上不正确。这是三元运算符的正确语法 -

condition ? expr1 : expr2 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

And return is not part of an expression. return is followed by an expression. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return)

并且返回不是表达式的一部分。返回后跟一个表达式。 (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return)

#4


2  

There is a way to return a value with a ternary statement. I use this often when programming in Swift, as I believe that it cleans up code. In my opinion, this is much better than using an if statement. It saves a ton of code too.

有一种方法可以使用三元语句返回值。我在Swift编程时经常使用它,因为我相信它可以清理代码。在我看来,这比使用if语句要好得多。它也节省了大量代码。

Consider this:

var compare = function(a,b) {
    return (a == b ? true : false);
}

Now test it with:

现在测试它:

console.log(compare(1,1));
console.log(compare(1,2));

Which evaluates to true and false respectively. While this is almost identical in Swift, I've verified that this Javascript code works.

其分别评估为真和假。虽然这在Swift中几乎完全相同,但我已经验证了这个Javascript代码是有效的。

#1


25  

The ternary operator evaluates to an expression and expressions can't contain a return statement (how would that behave if you were to assign the expression to a variable?). However, you could very well return the result of a ternary operator, i.e. return condition? returnValue1 : returnValue2;

三元运算符求值为表达式,表达式不能包含return语句(如果要将表达式赋值给变量,那将会如何表现?)。但是,您可以很好地返回三元运算符的结果,即返回条件? returnValue1:returnValue2;

On your specific point, I don't see why you would like to return. It looks like you're trying to do something only if a condition is fulfilled. A simple if statement would probably be more adequate there.

在你的具体观点,我不明白你为什么要回来。看起来你只是在条件得到满足的情况下才会做某事。一个简单的if语句可能更适合那里。

#2


7  

JavaScript (like many other languages) has expressions and statements. An expression must evaluate to something. A statement performs an action. Expressions can be used as statements, but the reverse is not true.

JavaScript(像许多其他语言一样)具有表达式和语句。表达式必须评估某事。声明执行操作。表达式可以用作语句,但反之则不然。

return is a statement. It does not evaluate to anything. You are using a ternary expression (a.k.a ternary operator), which has the syntax

return是一个声明。它没有评估任何东西。您正在使用具有语法的三元表达式(a.k.a三元运算符)

test ? expression1 : expression2

and evaluates to expression1 if the condition holds, and expression2 otherwise. This implies that expression1 and expression2 must themselves evaluate to something, and cannot be statements.

如果条件成立则计算表达式1,否则计算表达式2。这意味着expression1和expression2必须自己评估某些东西,而不能是语句。


Bottom line, you should use an if.

最重要的是,你应该使用if。

#3


4  

Because it is not syntactically correct. Here's the correct syntax for a ternary operator -

因为它在语法上不正确。这是三元运算符的正确语法 -

condition ? expr1 : expr2 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

And return is not part of an expression. return is followed by an expression. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return)

并且返回不是表达式的一部分。返回后跟一个表达式。 (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return)

#4


2  

There is a way to return a value with a ternary statement. I use this often when programming in Swift, as I believe that it cleans up code. In my opinion, this is much better than using an if statement. It saves a ton of code too.

有一种方法可以使用三元语句返回值。我在Swift编程时经常使用它,因为我相信它可以清理代码。在我看来,这比使用if语句要好得多。它也节省了大量代码。

Consider this:

var compare = function(a,b) {
    return (a == b ? true : false);
}

Now test it with:

现在测试它:

console.log(compare(1,1));
console.log(compare(1,2));

Which evaluates to true and false respectively. While this is almost identical in Swift, I've verified that this Javascript code works.

其分别评估为真和假。虽然这在Swift中几乎完全相同,但我已经验证了这个Javascript代码是有效的。