Javascript 三元运算符和赋值

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

Javascript ternary operator and assignment

javascriptvariable-assignmentternary

提问by faridz

I get unexpected result for this simple JavaScript assignment statement:

对于这个简单的 JavaScript 赋值语句,我得到了意想不到的结果:

var t = 1 == 1 ? 1 : 0;
undefined

I would have expected to get 1 assigned to v instead. Same result if you do

我本来希望将 1 分配给 v 。如果你这样做,结果相同

var t = (1 == 1 ? 1 : 0);
undefined

Can somebody explain why this does not work as expected?

有人可以解释为什么这不能按预期工作吗?

回答by Wayne

The result of evaluating var t = 1 == 1 ? 1 : 0;in, say, the Firebug console will be undefined. However, the value of twill be 1as expected. Try outputting tafter the assignment.

var t = 1 == 1 ? 1 : 0;例如,在 Firebug 控制台中评估的结果将为undefined. 但是, 的值t1如预期的那样。尝试t在分配后输出。

Firebug willprint the result when the variable declaration is on a separate line:

当变量声明在单独的行上时,Firebug打印结果:

var t;
t = 1 == 1 ? 1 : 0;

This is because the return value of an assignment operation is the value being assigned. However, when the varkeyword is present, what's returning is the value of the VariableStatement declaration, which behaves as follows:

这是因为赋值操作的返回值是被赋值的值。但是,当var关键字存在时,返回的是 VariableStatement 声明的值,其行为如下:

The production VariableStatement: varVariableDeclarationList; is evaluated as follows: Evaluate VariableDeclarationList. Return (normal, empty, empty).

生产VariableStatement: var VariableDeclarationList; 评估如下:评估 VariableDeclarationList。返回(正常,空,空)。

Where Return (normal, empty, empty).refers to a type recognized by JavaScript internally, not something that would be printed to the console.

WhereReturn (normal, empty, empty).指的是 JavaScript 在内部识别的类型,而不是会打印到控制台的内容。

Further reading:

进一步阅读:

http://ecma262-5.com/ELS5_HTML.htm#Section_12.2

http://ecma262-5.com/ELS5_HTML.htm#Section_12.2

回答by Felix Kling

It works perfectly:

它完美地工作:

> var t = 1 == 1 ? 1 : 0;
undefined
> t
1

You could say that the return value of the assignment operationis undefined, not the value of t.

你可以说赋值操作的返回值是undefined,而不是 的值t



Edit:But actually if I read the specification correctly, it seems that it should return the value of the expression.

编辑:但实际上,如果我正确阅读了规范,它似乎应该返回表达式的值。

As @T.J. Crowder mentioned, it seems the varis responsible for the undefinedvalue. But that does not mean that you should not use var. The code you wrote is 100% correct.

正如@TJ Crowder 所提到的,似乎var是对undefined价值负责。但这并不意味着您不应该使用var. 您编写的代码是 100% 正确的。

This goes more into the inner workings of the language and I think that is not what you are interested in. Bur for more information about that, have a look at the comments.

这更多地涉及语言的内部工作原理,我认为这不是您感兴趣的内容。有关更多信息,请查看评论。

回答by Andrei

In old javascript parsers we need to conclude the condition in parentheses:

在旧的 javascript 解析器中,我们需要在括号中结束条件:

var t = (1 == 1) ? 1 : 0;

回答by gor

This code works fine:

这段代码工作正常:

var t = 1 == 1 ? 1 : 0;
alert (t);

Check here. It shows 1.

检查这里。它显示1