简体   繁体   English

Javascript内联if语句

[英]Javascript inline if statement

I had a bit of a weird result in the javascript console. 我在javascript控制台中有一些奇怪的结果。 I was trying to look for an alternative (more readable) version of the ternary operator, just for fun. 我试图寻找三元运算符的替代(更易读)版本,只是为了好玩。 Typing: 打字:

{ if(3===4) {5} else {6} }

Evaluates in my console to 6, but for some reason, I can not assign it to a variable, so running: 在我的控制台中评估为6,但由于某种原因,我无法将其分配给变量,因此运行:

let a = { if(3===4) {5} else {6} }

Does not let me store it to the variable directly. 不要让我直接将它存储到变量中。 So my main question is, if this block is returning something, why can't I assign it? 所以我的主要问题是,如果这个块返回了什么,为什么我不能分配它?

The fact that blocks (and other statements) return values isn't accessible to your code. 您的代码无法访问块(和其他语句)返回值的事实。 Consoles can see the result, and it exists at a language specification level, but not in your code. 控制台可以查看结果,它存在于语言规范级别,但不存在于代码中。

Your options are the conditional operator¹ (which is quite readable when you're used to it, but you've said you're looking for alternatives to it) or the if / else assigning to a in both parts: 您的选择是有条件的operator¹(当你使用它这是相当可读的,但你说你正在寻找替代它),或者if / else分配给a在两个部分:

let a;
if (3 === 4) {
    a = 5;
} else {
    a = 6;
}

Or you could use an inline function (IIFE): 或者您可以使用内联函数(IIFE):

let a = (() => { if (3 === 4} return 5 else return 6; })();

There is also a proposal being floated for " do expressions ", which would look like this: 还有一个提议浮出“ do表达式 ”,如下所示:

// Currently a proposal, not implemented in engines yet
let a = do { if (3 === 4) 5; else 6; };

That proposal is at Stage 1 of the process , so it may or may not progress, and if it progresses it could change markedly before doing so. 该提案处于流程的第1阶段,因此它可能会或可能不会进展,如果它进展,它可能会在此之前发生显着变化。


¹ Although you see "the ternary operator" a lot, the proper name is the conditional operator. ¹尽管你经常看到“三元运算符”,但正确的名称是条件运算符。 It is a ternary operator (an operator accepting three operands), and currently JavaScript's only ternary operator, but that could change someday. 它是一个三元运算符(一个接受三个操作数的运算符),目前是JavaScript唯一的三元运算符,但有一天可能会改变。 :-) :-)

使用三元运算符 ,因为你不能分配if语句:

let a = 3 === 4 ? 5 : 6;

The reason this doesn't work is because if is, as you pointed out, a statement. 这不工作的原因是因为if是,正如你所指出,声明。 An assignment requires an expression holding a value to be assigned. 赋值需要一个包含要分配的值的表达式 A statement doesn't ( per se ) have a value--it simply performs some side-effect instead. 声明( 本身 )没有一个值 - 它只是执行一些副作用。

What you can use if you want to assign a value conditionally is the ternary conditional operator, which is an expression: 如果要有条件地分配值,可以使用的是三元条件运算符,它是一个表达式:

let a = (3 === 4 ? 5 : 6)

At this point you're probably wondering, why does the console print a value if a statement merely performs side-effects? 此时你可能想知道,如果一个语句只是执行副作用,为什么控制台会打印一个值? This is because statements have a completion value , which for an if statement is the completion value of the corresponding branch, and for an expression statement (which is what the 5 and 6 inside the blocks are), is whatever the expression evaluates to. 这是因为语句有一个完成值 ,对于if语句,它是相应分支的完成值,对于表达式语句(这是块内部的56 ),是表达式求值的内容。 This is mainly useful for when you want to evaluate an expression in the console and see what it produces, without having to issue a console.log() each time you do. 当你想要在控制台中评估表达式并查看它产生的内容时,这非常有用,而不必每次都发出console.log()

It's not returning anything. 它没有返回任何东西。 if is not an expression, it's a statement; if不是表达,那就是陈述; it has no value. 它没有任何价值。

The point of the ternary operator was to provide a more readable version of this: 三元运算符的要点是提供一个更易读的版本:

let a;
if (condition) {
    a = 1;
}
else {
    a = 2;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM