Expressions versus statements in JavaScript

Posted Rosendolu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Expressions versus statements in JavaScript相关的知识,希望对你有一定的参考价值。

Statements and expressions

An expression produces a value and can be written wherever a value is expected.

Expressions that look like statements

  • Expressions that look like statements

    javascript has stand-alone blocks? It might surprise you that JavaScript has blocks that can exist on their own (as opposed to being part of a loop or an if statement). The following code illustrates one use case for such blocks: You can give them a label and break from them.

        function test(printTwo) {
          printing: {
              console.log("One");
              if (!printTwo) break printing;
              console.log("Two");
          }
          console.log("Three");
      }
  • Function expression versus function declaration
    function expressions
function() {}
function fn() {}

Using object literals and function expressions as statements

  • eval parses its argument in statement context. If you want eval to return an object, you have to put parentheses around an object literal.

      > eval("{ foo: 123 }")
      123
      > eval("({ foo: 123 })")
      { foo: 123 }
  • Immediately invoked function expressions (IIFEs)

      > (function () { return "abc" }())
      'abc'

    If you omit the parentheses, you get a syntax error (function declarations can’t be anonymous):

      > function () { return "abc" }()
      SyntaxError: function statement requires a name

    If you add a name, you also get a syntax error (function declarations can’t be immediately invoked):

      > function foo() { return "abc" }()
      SyntaxError: syntax error

    Another way of guaranteeing that an expression is parsed in expression context is a unary operator such as + or !. But, in contrast to parentheses, these operators change the result of the expression. Which is OK, if you don’t need it:

      > +function () { console.log("hello") }()
      hello
      NaN

    NaN is the result of applying + to undefined, the result of calling the function. Brandon Benvie mentions another unary operator that you can use: void [2]:

      > void function () { console.log("hello") }()
      hello
      undefined
  • Concatenating IIFEs

    When you concatenate IIFEs, you must be careful not to forget semicolons:

      (function () {}())
      (function () {}())
      // TypeError: undefined is not a function

    This code produces an error, because JavaScript thinks that the second line is an attempt to call the result of the first line as a function. The fix is to add a semicolon:

      (function () {}());
      (function () {}())
      // OK

    With operators that are only unary (plus is both unary and binary), you can omit the semicolon, because automatic semicolon insertion kicks in.

      void function () {}()
      void function () {}()
      // OK

    JavaScript inserts a semicolon after the first line, because void is not a valid way of continuing this statement [3].

原文:Expressions versus statements in JavaScript

以上是关于Expressions versus statements in JavaScript的主要内容,如果未能解决你的问题,请参考以下文章

Physical (Raw) Versus Logical Backups

Strong AI Versus Weak AI

Apollo versus Pan(位运算)

在处理状态超时时优雅地退出 gen_statem

gen_server 和 gen_statem 有啥区别?

SysML Value Properties versus UML Attributes