码迷,mamicode.com
首页 > 编程语言 > 详细

Expressions versus statements in JavaScript

时间:2019-01-10 23:22:48      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:statement   exist   UNC   express   another   hello   abc   auto   typeerror   

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

标签:statement   exist   UNC   express   another   hello   abc   auto   typeerror   

原文地址:https://www.cnblogs.com/rosendolu/p/10251133.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!