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 ...
随机推荐
- 洛谷P3258 松鼠的新家
树上差分 这应该是一道很简单的树上差分了..就是问每个点被覆盖了多少次. 要注意我们最后dfs后,要把除第一个节点以外的所有点的-1,因为有些点作为起点和终点覆盖了两次,按照题目意思是不用覆盖两次的. ...
- python中的map函数
def f(x): return x * x """将一个全是数字的list变成平方形式""" def f2(): ls = [1, 2, ...
- 将xml文件由格式化变为压缩字符串
标签:去除xml文件的空格 有些时候解析xml文件,要求读取的字符串必须是压缩后的xml文件,不能有多余的空格.考虑到在<>标签内包含空格和大于号的情况,写了以下的转换方式. 传入的是压缩 ...
- linux 开放80端口
必须确保两块都开放 1.云服务器-->安全组开放 比如百度云服务器: 2.linux内置防火墙开放 注意:此处如果不设置开放,即时云端开放了也没用,如果同时存在 80 (拒绝) 80(允许) ...
- 【php】php从多个数组中取出最大的值
function _arr_max($arr = []){ if(func_num_args() > 1){ $result = []; foreach(func_get_args() as $ ...
- GNOME禁用GDM中night-light功能
Night-light feature is enabled also in GDM screen, see here : https://bugzilla.gnome.org/show_bug.cg ...
- A1140. Look-and-say Sequence
Look-and-say sequence is a sequence of integers as the following: D, D1, D111, D113, D11231, D112213 ...
- (转)visual stdio 书签功能介绍
http://www.mycode.net.cn/tools/1615.html 使用 Visual Studio 开发过程中,你很容易遇到一种情况就是多个文件来回的切换,在每一块实现不同的业务,打开 ...
- (转)你应该知道的RPC原理
背景:对于项目中的RPC框架,仅仅停留在使用层面,对于其底层的实现原理不是很清楚.这样的后果是很危险的,对于面试官来说,跟不知道这个东西一样. 转载自:https://www.cnblogs.com/ ...
- Summary of Java basics review data
1.标识符 用于命名程序的对象,如方法名,变量名,规则是: a.大小写敏感 b.由英文字符,文字字符,美元符号,下划线和数字组成,但不能以数字开头 c.不能是关键字 2.%:求余运算符 ...