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. bzoj 1264: [AHOI2006]基因匹配Match (树状数组优化dp)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1264 思路: n大小为20000*5,而一般的dp求最长公共子序列复杂度是 n*n的,所以我 ...

  2. CF1117F. Crisp String

    题意 给出表格,表示字母间"相邻"关系 保证给定字符串所有相邻字母"相邻" 删除某种字母时 要保证其两侧字母"相邻" 即删除后还是相邻字母& ...

  3. 「HNOI2016」最小公倍数

    链接 loj 一道阔爱的分块 题意 边权是二元组(A, B),每次询问u, v, a, b,求u到v是否存在一条简单路径,使得各边权上\(A_{max} = a, B_{max} = b\) 分析 对 ...

  4. JVM垃圾收集(Java Garbage Collection / Java GC)

    JVM垃圾收集(Java Garbage Collection / Java GC) Java7 Java8 JDK1.8之后将最初的永久代取消了,由元空间取代. 堆内存调优简介 public sta ...

  5. IT人员必须掌握的10项软技能

    现代企业的IT部门员工在具备技术能力的同时,还需要掌握一些软技能.现在来看这些软技能其实并不是什么新鲜事物,只是IT人员所需的这些软技能的范围与数量正在不断增加着. IT人员需要具备哪些技术能力,要取 ...

  6. JavaWeb架构发展

    原文:JavaWeb项目为什么我们要放弃jsp?为什么要前后端解耦?为什么要前后端分离?2.0版,为分布式架构打基础 前戏 前后端分离已成为互联网项目开发的业界标准使用方式,通过Nginx + Tom ...

  7. hexo博客添加功能

    设置Hexo主题模式 Hexo主题中,有三种不同的模式,通过切换模式,让NexT主题显示不一样的样式.在NexT根目录下有一个同样名称为_config.yml,为了区分hexo根目录下的_config ...

  8. photoshop学习3

    一.仿制图章工具 快捷键:S. 操作:先按住ALT键,再点击图片的一个地方,然后松开ALT和鼠标(这叫取样).之后到画布的另一个地方用鼠标绘画. 特点:绘画出和取样点一样的图像.这个工具原样复制了取样 ...

  9. hdu 1024 Max Sum Plus Plus(m段最大和)

    Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b ...

  10. Python学习day2 while循环&格式化输出&运算符

    day2 运算符-while循环 1.while循环 while循环基本结构; while 条件:      结果    # 如果条件为真,那么循环则执行    # 如果条件为假,那么循环不执行 de ...