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. CODEFORCES掉RATING记 #5

    ​ 比赛:Codeforces Round #429 (Div. 2) ​ 时间:2017.8.1晚 这次感觉状态不好,就去打div2了 ​ A:有\(26\)种颜色的气球,每种的数量不一样,你要把这 ...

  2. 【BZOJ1426】收集邮票 期望DP

    题目大意 有\(n\)种不同的邮票,皮皮想收集所有种类的邮票.唯一的收集方法是到同学凡凡那里购买,每次只能买一张,并且买到的邮票究竟是\(n\)种邮票中的哪一种是等概率的,概率均为\(\frac{1} ...

  3. Codeforces Round #530 (Div. 2) F (树形dp+线段树)

    F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...

  4. codeforces 242E - XOR on Segment (线段树 按位数建树)

    E. XOR on Segment time limit per test 4 seconds memory limit per test 256 megabytes input standard i ...

  5. PKUWC2019 凉凉记

    请配合 BGM 食用. 菜就是菜,说什么都是借口. Day 0 前一天先到纪中报道,高铁上打了一会单机膈膜,然后又打了一遍 \(FFT\) 板子,就到了中山. 到了后,发现气温骤然升高,马上 脱 换裤 ...

  6. 【BZOJ4316】小C的独立集(动态规划)

    [BZOJ4316]小C的独立集(动态规划) 题面 BZOJ 题解 考虑树的独立集求法 设\(f[i][0/1]\)表示\(i\)这个点一定不选,以及\(i\)这个点无所谓的最大值 转移\(f[u][ ...

  7. 「TJOI2015」概率论 解题报告

    「TJOI2015」概率论 令\(f_i\)代表\(i\)个点树形态数量,\(g_i\)代表\(i\)个点叶子个数 然后列一个dp \[ f_i=\sum_{j=0}^{i-1} f_j f_{i-j ...

  8. python爬虫解析库学习

    一.xpath库使用: 1.基本规则: 2.将文件转为HTML对象: html = etree.parse('./test.html', etree.HTMLParser()) result = et ...

  9. OO第三阶段纪实

    $0 写在前面 万里长征已过大半,即将迎来胜利的曙光.一路走来,经历过种种艰难,体会颇深.希望能记录下这篇博文,来总结这一个月来的收获与感悟. $1 规格化设计的发展历史 上世纪50年代,软件伴随着第 ...

  10. Flask 键盘事件

    <div class="container" style="margin-top: 300px; "> <div class="ro ...