简体   繁体   English

有人可以解释使用语句值Javascript ECMAScript吗?

[英]Can somebody explain the use of statement values Javascript ECMAScript?

I am new to Javascript and have noticed that statements have values: 我是Javascript的新手,并注意到语句有值:

> x = 1
<- 1

> if (false) {
    x=1;
  } 
  else { x=2 };
<- 2

Could somebody explain why statements have values what they are used for in real applications, since functions need to return values explicitly via return . 有人可以解释为什么语句具有它们在实际应用程序中使用的值,因为函数需要通过return显式返回值。

Is this related to the notion of completions, which are associated with values, irrespective of their type (Normal or Abrupt) ? 这与完成的概念有关,它与价值相关,不论其类型(正常还是突然)?

Normal completion values primarily come up in user code via eval , though even then it's not super common. 正常完成值主要通过eval出现在用户代码中,尽管它们并不常见。 The return value of eval is the final completion value of the executed statements. eval的返回值是已执行语句的最终完成值。 Since eval executes a list of statements, when you run eval("4") === 4 for instance, you're not evaluating an expression for a value specifically, automatic-semicolon-insertion is kicking in, so you're actually running eval("4;") and then getting the completion value of that statement. 由于eval执行一个语句列表,例如当你运行eval("4") === 4 ,你没有专门评估一个值的表达式,自动分号插入正在进行,所以你实际上是运行eval("4;") ,然后获取该语句的完成值。

In the future, completion-value semantics will also likely affect the do expression proposal which would allow for code like 在将来,完成值语义也可能会影响do表达式提议 ,这将允许代码类似

const value = do {
  if (false) {
    1;
  } else { 
    2;
  }
};

value === 2;

You could almost look at your examples as a nice side-effect of the general completion value semantics in the language, as well. 您几乎可以将您的示例视为语言中一般完成值语义的一个很好的副作用。 When you throw or return from a function, the spec language itself for instance, still traverses the whole function. 当您从函数throwreturn ,例如,规范语言本身仍然遍历整个函数。 Returning or throwing essentially says "the completion value is this value" with the type "return" or "throw". 返回或抛出基本上说“完成值是这个值”,类型为“return”或“throw”。 So if you had 所以,如果你有

function fn()
  if (false) {
    return 1;
  } else { 
    2;
  } 
}

one branch of the "if" produces an abrupt completion with value 1 , and the other produces a normal completion with a value 2 . “if”的一个分支以值1产生突然完成,另一个分支产生值为2的正常完成。

When execution (from the spec standpoint), gets to the end of the function itself, it will say "is this a return/throw, if so, propagate it to the caller of the function. If it is a normal completion, it will just discard the value and return undefined . 当执行时(从spec的角度来看),到达函数本身的末尾,它会说“这是一个返回/抛出,如果是这样,将它传播给函数的调用者。如果它是正常完成,它将只是丢弃该值并返回undefined

So really, the fact that there is a completion value is exactly the same as having a return value or throwing an exception value, from the standpoint of the spec, it just happens to discard the value in most cases if it is a non-abrupt completion. 实际上,存在完成值的事实与具有返回值或抛出异常值完全相同,从规范的角度来看,它恰好在大多数情况下丢弃该值,如果它是非突然的完成。

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

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