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
eval
parses its argument in statement context. If you wanteval
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的更多相关文章
- 【转】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 ...
随机推荐
- Codeforces264 B. Good Sequences
Codeforces题号:#264B 出处: Codeforces 主要算法:DP 难度:4.8 思路分析: 这题DP太难了…… 最终的解法是,令f[i]表示存在因子i的一个数作为子序列结尾的子序列的 ...
- pip详解
pip是一个安装和管理 Python 包的工具.python安装包的工具有easy_install, setuptools, pip,distribute等,pip是Python官方推荐的包管理工具 ...
- 【XSY2754】求和 莫比乌斯反演 杜教筛
题目描述 给你\(n,p\),求 \[ \sum_{i=1}^n\sum_{j=1}^i\sum_{k=1}^i\gcd(i,j,k)\mod p \] \(n\leq {10}^9\) 题解 \[ ...
- ionic更改端口号
—— 重新指定端口号为8888 serve [options] ............................... 启动本地服务器进行开发测试 dev/testing [--console ...
- python3 集合set
set是一种集合的数据类型,使用{}表示 集合中元素是无序的,并且不可重复,集合最重要的作用就是可以去重 set是不可哈希的,set中的元素必须是可哈希的 可以切片,可以迭代 交集.并集.差集.对称差 ...
- Write less code
If you find yourself writing a lot of code to do something simple, you're probably doing it wrong. A ...
- One Person Game ZOJ - 3329(期望dp, 数学)
There is a very simple and interesting one-person game. You have 3 dice, namely Die1, Die2 and Die3. ...
- Java复习总结——数据类型
包装类型 八个基本类型: Boolean/boolean/1 Byte/byte/8 Character/char/16 Short/short/16 Integer/int/32 Float/flo ...
- 百度地图API示例:使用vue添加删除覆盖物
1.index.html <script type="text/javascript" src="http://api.map.baidu.com/api?v=2. ...
- Http请求报头设置
1.添加一个SetHeaderValue方法: public static void SetHeaderValue(WebHeaderCollection header, string name, s ...