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
evalparses its argument in statement context. If you wantevalto 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
NaNis 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的更多相关文章
- 【转】Expressions versus statements in JavaScript
原文地址:http://www.2ality.com/2012/09/expressions-vs-statements.html Update 2012-09-21: New in Sect. 4: ...
- JavaScript简易教程(转)
原文:http://www.cnblogs.com/yanhaijing/p/3685304.html 这是我所知道的最完整最简洁的JavaScript基础教程. 这篇文章带你尽快走进JavaScri ...
- 每个JavaScript工程师都应懂的33个概念
摘要: 基础很重要啊! 原文:33 concepts every JavaScript developer should know 译文:每个 JavaScript 工程师都应懂的33个概念 作者:s ...
- JavaScript简易教程
这是我所知道的最完整最简洁的JavaScript基础教程. 这篇文章带你尽快走进JavaScript的世界——前提是你有一些编程经验的话.本文试图描述这门语言的最小子集.我给这个子集起名叫做“Java ...
- 每个 JavaScript 工程师都应懂的33个概念
简介 这个项目是为了帮助开发者掌握 JavaScript 概念而创立的.它不是必备,但在未来学习(JavaScript)中,可以作为一篇指南. 本篇文章是参照 @leonardomso 创立,英文版项 ...
- JavaScript 中表达式和语句的区别
1.语句和表达式 JavaScript中的表达式和语句是有区别的.一个表达式会产生一个值,它可以放在任何需要一个值的地方,比如,作为一个函数调用的参数.下面的每行代码都是一个表达式: myvar3 + ...
- 对JavaScript优化及规范的一些感想
变量...... 1.一个变量只存一种类型的数据,2.尽量减少对隐式转换的依赖,这样可增强程序的可读性,日后修改程序时不至于混乱,3.使用匈牙利命名法,4.使用局部变量时记得加 var 进行声明,不然 ...
- (译)详解javascript立即执行函数表达式(IIFE)
写在前面 这是一篇译文,原文:Immediately-Invoked Function Expression (IIFE) 原文是一篇很经典的讲解IIFE的文章,很适合收藏.本文虽然是译文,但是直译的 ...
- [转]Javascript中的自执行函数表达式
[转]Javascript中的自执行函数表达式 本文转载自:http://www.ghugo.com/javascript-auto-run-function/ 以下是正文: Posted on 20 ...
随机推荐
- bzoj 1029: [JSOI2007]建筑抢修 (优先队列)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1029 思路: 按结束时间排序,优先选结束时间短的,选完后扔到优先队列里(大的优先),如果选到 ...
- jqGrid 手册 - 搜索
搜索 类型: colModel Options Toolbar Searching Custom Searching Single field searching Advanced Searching ...
- 用keras实现基本的文本分类任务
数据集介绍 包含来自互联网电影数据库的50000条影评文本,对半拆分为训练集和测试集.训练集和测试集之间达成了平衡,意味着它们包含相同数量的正面和负面影评,每个样本都是一个整数数组,表示影评中的字词. ...
- Python面试指南
1.Python基本语法 1.@staticmethod 和 @classmethod Python中有三种方法,实例方法.类方法(@classmethod).静态方法(@staticmethod). ...
- ELK部署详解--logstash
logstash.yml # Settings file in YAML## Settings can be specified either in hierarchical form, e.g.:# ...
- hdu 1978 How many ways(记忆化搜索)
这是一个简单的生存游戏,你控制一个机器人从一个棋盘的起始点(1,1)走到棋盘的终点(n,m).游戏的规则描述如下:1.机器人一开始在棋盘的起始点并有起始点所标有的能量.2.机器人只能向右或者向下走,并 ...
- 20165223 week2学习查漏补缺
标识符.字符集.关键字 基本数据类型 逻辑类型:boolean 常量:true.false 变量:boolean赋值 整数类型:byte.short.long.int 注意long型后缀L Java没 ...
- centos7安装saltstack
环境是Cenos7 saltstack-master:192.168.0.140 saltstack-minion:192.168.0.141 安装epel yum源 yum -y install e ...
- C++ const 理解
转载自:https://www.cnblogs.com/jiabei521/p/3335676.html 如果函数需要传入一个指针,面试官可能会问是否需要为该指针加上const,把const加在指针不 ...
- 斯坦福大学公开课机器学习: neural networks learning - autonomous driving example(通过神经网络实现自动驾驶实例)
使用神经网络来实现自动驾驶,也就是说使汽车通过学习来自己驾驶. 下图是通过神经网络学习实现自动驾驶的图例讲解: 左下角是汽车所看到的前方的路况图像.左上图,可以看到一条水平的菜单栏(数字4所指示方向) ...