Javascript 为什么我们不能在三元运算符中返回?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35231609/
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
Why can't we have return in the ternary operator?
提问by Guruprasad Rao
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.
说我有一个简单的form
,我想检查是否form
已经改变。如果它改变了submit
它会阻止表单提交,所以我使用 return 而不是 usingif-else
我尝试使用的语句,ternary operation
但不幸的是我遇到了错误,Uncaught SyntaxError: Unexpected token return
但我不明白为什么会出现这个错误?三元运算仅用于赋值吗?不确定这部分。下面只是我试图做的一个例子。
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>
回答by Aaron
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 语句(如果将表达式分配给变量会怎样?)。但是,您可以很好地返回三元运算符的结果,即return condition? 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
那里,一个简单的陈述可能更合适。
回答by Igor Raush
JavaScript (like many other languages) has expressionsand 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
是一个声明。它不评估任何东西。您正在使用三元表达式(又名三元运算符),其语法为
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.
并评估expression1
条件是否成立,expression2
否则。这意味着expression1
and expression2
must 本身评估某事,并且不能是陈述。
Bottom line, you should use an if
.
最重要的是,您应该使用if
.
回答by Kazekage Gaara
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
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)
并且return
不是表达式的一部分。return
后面跟着一个表达式。( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return)
回答by Alec O
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.
分别评估为true
和false
。虽然这在 Swift 中几乎相同,但我已经验证了这段 Javascript 代码有效。
回答by bee
this is function that has a return with a ternary
这是具有三元返回值的函数
function tst(x, y = 1) {
x == 0 ? x = 1 : y *= x;
return x<=1? y:tst(x-1,y)
}