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的更多相关文章

  1. 【转】Expressions versus statements in JavaScript

    原文地址:http://www.2ality.com/2012/09/expressions-vs-statements.html Update 2012-09-21: New in Sect. 4: ...

  2. JavaScript简易教程(转)

    原文:http://www.cnblogs.com/yanhaijing/p/3685304.html 这是我所知道的最完整最简洁的JavaScript基础教程. 这篇文章带你尽快走进JavaScri ...

  3. 每个JavaScript工程师都应懂的33个概念

    摘要: 基础很重要啊! 原文:33 concepts every JavaScript developer should know 译文:每个 JavaScript 工程师都应懂的33个概念 作者:s ...

  4. JavaScript简易教程

    这是我所知道的最完整最简洁的JavaScript基础教程. 这篇文章带你尽快走进JavaScript的世界——前提是你有一些编程经验的话.本文试图描述这门语言的最小子集.我给这个子集起名叫做“Java ...

  5. 每个 JavaScript 工程师都应懂的33个概念

    简介 这个项目是为了帮助开发者掌握 JavaScript 概念而创立的.它不是必备,但在未来学习(JavaScript)中,可以作为一篇指南. 本篇文章是参照 @leonardomso 创立,英文版项 ...

  6. JavaScript 中表达式和语句的区别

    1.语句和表达式 JavaScript中的表达式和语句是有区别的.一个表达式会产生一个值,它可以放在任何需要一个值的地方,比如,作为一个函数调用的参数.下面的每行代码都是一个表达式: myvar3 + ...

  7. 对JavaScript优化及规范的一些感想

    变量...... 1.一个变量只存一种类型的数据,2.尽量减少对隐式转换的依赖,这样可增强程序的可读性,日后修改程序时不至于混乱,3.使用匈牙利命名法,4.使用局部变量时记得加 var 进行声明,不然 ...

  8. (译)详解javascript立即执行函数表达式(IIFE)

    写在前面 这是一篇译文,原文:Immediately-Invoked Function Expression (IIFE) 原文是一篇很经典的讲解IIFE的文章,很适合收藏.本文虽然是译文,但是直译的 ...

  9. [转]Javascript中的自执行函数表达式

    [转]Javascript中的自执行函数表达式 本文转载自:http://www.ghugo.com/javascript-auto-run-function/ 以下是正文: Posted on 20 ...

随机推荐

  1. 【CF633D】Fibonacci-ish

    题目描述 小y最近迷上了fibonacci数列,他定义了一种数列叫类fibonacci数列: 1.这个数列包含至少\(2\)个元素 2.\(f_0\)和\(f_1\)是任意选取的 3.\(f_{n+2 ...

  2. 解决nginx发布网站跨目录访问

    解决nginx发布网站跨目录访问(thinkphp5+lnmp) 到:usr/local/nginx/conf/vim fastcgi.cof 把最后一行加上井号#注释掉保存重启 restart 参考 ...

  3. windows 下项目打包、备份、覆盖、md5check

    工具从网络自行下载,目前我存储在网盘上,可下载后调用 更新包打包.创建md5,压缩成.zip 现有项目按日期备份 覆盖项目并做md5check @echo off rem ============== ...

  4. 关于360插件化Replugin Activity动态修改父类的字节码操作

    近期在接入360插件化方案Replugin时,发现出现崩溃情况. 大概崩溃内容如下: aused by: java.lang.ClassNotFoundException: Didn't find c ...

  5. 【BZOJ5294】[BJOI2018]二进制(线段树)

    [BZOJ5294][BJOI2018]二进制(线段树) 题面 BZOJ 洛谷 题解 二进制串在模\(3\)意义下,每一位代表的余数显然是\(121212\)这样子交替出现的. 其实换种方法看,就是\ ...

  6. 【BZOJ4832】抵制克苏恩(矩阵快速幂,动态规划)

    [BZOJ4832]抵制克苏恩(矩阵快速幂,动态规划) 题面 BZOJ 题解 一模一样 #include<iostream> #include<cstdio> using na ...

  7. Dos 批处理 Shutdown

    第一步: win + R,打开"运行" 第二步: 输入cmd 第三步: 输入color a或color b改成自己喜欢的颜色 第四步: 在shutdown后面输入不同的命令达到不同 ...

  8. TCP的三次握手和四次挥手图解

     1. TCP建立连接的三次握手 (1)第一次握手:Client将标志位SYN置为1,随机产生一个值seq=J,并将该数据包发送给Server,Client进入SYN_SENT状态,等待Server确 ...

  9. 分布式链路跟踪 Sleuth 与 Zipkin【Finchley 版】

    原创: dqqzj SpringForAll社区 今天 Spring Cloud Sleuth Span是基本的工作单位. 例如,发送 RPC是一个新的跨度,就像向RPC发送响应一样. 跨度由跨度唯一 ...

  10. Hadoop安装错误总结

    Master的NodeManager/DateNode未启动 日志中未出现任何错误 正常现象,如需在Master中启动可在slave文件中 slaves localhost slave01 slave ...