JavaScript & Automatic Semicolon Insertion
JavaScript & Automatic Semicolon Insertion
ECMA 262 真香警告️
https://www.ecma-international.org/ecma-262/6.0/index.html#sec-automatic-semicolon-insertion
Certain ECMAScript statements (empty statement, let, const, import, and export declarations, variable statement, expression statement, debugger statement, continue statement, break statement, return statement, and throw statement) must be terminated with semicolons. Such semicolons may always appear explicitly in the source text. For convenience, however, such semicolons may be omitted from the source text in certain situations. These situations are described by saying that semicolons are automatically inserted into the source code token stream in those situations.
https://www.ecma-international.org/ecma-262/5.1/#sec-7.9
Certain ECMAScript statements (empty statement, variable statement, expression statement, do-while statement, continue statement, break statement, return statement, and throw statement) must be terminated with semicolons. Such semicolons may always appear explicitly in the source text. For convenience, however, such semicolons may be omitted from the source text in certain situations. These situations are described by saying that semicolons are automatically inserted into the source code token stream in those situations.
wiki
https://en.wikibooks.org/wiki/JavaScript/Automatic_semicolon_insertion

- 不要在 return 后换行,不要在一条语句中换行;
- 不要将开括号
{放在单独的一行上

i = 3;
// 3
if (i === 5)
// assuming a semicolon here
console.log(`if`)
else
console.log(`else`);
// else
if (i === 5)
// assuming a semicolon here
console.log(`if`);
else
console.log(`else`);
// else
i = 5;
// 5
if (i === 5)
// assuming a semicolon here
console.log(`if`)
else
console.log(`else`);
// if
if (i === 5)
// assuming a semicolon here
console.log(`if`);
else
console.log(`else`);
// if
分步 OK

// "use strict";
let type = "Literal"
let name = 5
let node = {
type: "Identifier",
name: "foo"
}
// 隔开 OK
({ type, name } = node)
console.log(type)
console.log(name)
// Identifier
// foo
同步 bug

"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-07-29
* @modified
*
* @description Automatic Semicolon Insertion
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link
* @solutions
*
*/
const log = console.log;
// IIFE
(() => {
"use strict";
let type = "Literal"
let name = 5
let node = {
type: "Identifier",
name: "foo"
}
({ type, name } = node)
// Uncaught ReferenceError: Cannot access 'node' before initialization
console.log(type)
console.log(name)
})();
refs
https://www.zhihu.com/question/270095202
https://www.zhihu.com/question/270095202/answer/1368566113
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
JavaScript & Automatic Semicolon Insertion的更多相关文章
- The Dangers of JavaScript’s Automatic Semicolon Insertion
Although JavaScript is very powerful, the language’s fundamentals do not have a very steep learning ...
- JavaScript Semicolon Insertion
JavaScript Semicolon Insertion https://blog.izs.me/2010/12/an-open-letter-to-javascript-leaders-rega ...
- auto semicolon insertion 自动分号补齐的坑
今天发现js自动分号补齐的坑,来看如下两段代码: function Hello(){ return { name: ’JavaScript’ }; } alert(Hello()); //输出unde ...
- JavaScript简易教程(转)
原文:http://www.cnblogs.com/yanhaijing/p/3685304.html 这是我所知道的最完整最简洁的JavaScript基础教程. 这篇文章带你尽快走进JavaScri ...
- JavaScript简易教程
这是我所知道的最完整最简洁的JavaScript基础教程. 这篇文章带你尽快走进JavaScript的世界——前提是你有一些编程经验的话.本文试图描述这门语言的最小子集.我给这个子集起名叫做“Java ...
- 【转】Expressions versus statements in JavaScript
原文地址:http://www.2ality.com/2012/09/expressions-vs-statements.html Update 2012-09-21: New in Sect. 4: ...
- 读《编写可维护的JavaScript》第一章总结
第一章 基本的格式化 1.4 ① 换行 当一行长度到达了单行最大的字符限制时,就需要手动将一行拆成俩行.通常我们会在运算符后换行,下一行会增加俩个层级的缩进. // 好的做法: 在运算符后换行,第二行 ...
- JavaScript 常见错误
1. 严格缩进 JavaScript 会自动添加句末的分号,导致一些难以察觉的错误 return { key: value }; // 相当于 return; { key: value }; 2. 括 ...
- 良好的JavaScript编码风格(语法规则)
编码风格 1.概述 "编程风格"(programming style)指的是编写代码的样式规则.不同的程序员,往往有不同的编程风格. 有人说,编译器的规范叫做"语法规则& ...
随机推荐
- 在线配置热加载配置 go-kratos.dev 监听key
paladin https://v1.go-kratos.dev/#/config-paladin example Service(在线配置热加载配置) # service.go type Servi ...
- CAS客户端和服务器配置https证书
关于如何生成https证书可以看这篇文章: java生成Https证书,及证书导入的步骤和过程 下面整理cas如何整合https: cas服务器端部署(TLS[https]) 1.生成证书: 参照ja ...
- Cisco动态路由(rip)
接Cisco静态路由,讨论一下Cisco动态路由. 实验环境布置 命令布置动态路由 Router0: Router>enable Router#configure terminal Router ...
- linux-Navicat连接linux远程数据
linux-Navicat连接linux远程数据 (一)登陆数据库 (二)创建用户用于远程连接 GRANT ALL PRIVILEGES ON *.* TO '账号'@'%' IDENTIFIED B ...
- poj 3278 Catch That Cow(记忆化广度优先搜索)
题意: 0到N的数轴上,每次可以选择移动到x-1,x+1,2*x,问从n移动到k的最少步数. 思路: 同时遍历三种可能并记忆化入队即可. Tips: n大于等于k时最短步数为n-k. 在移动的过程中可 ...
- Codeforces Round #579 (Div. 3) D2. Remove the Substring (hard version) (思维,贪心)
题意:给你一个模式串\(t\),现在要在主串\(s\)中删除多个子串,使得得到的\(s\)的子序列依然包含\(t\),问能删除的最长子串长度. 题解:首先,我们不难想到,我们可以选择\(s\)头部到最 ...
- 2020ICPC·小米 网络选拔赛第一场 A.Intelligent Warehouse (DP)
题意:给你一组数,选一些数出来组成一个排列,使得每个数都能被前一个数整除,求排列的最大元素. 题解:我们先用欧拉筛筛出\(1e7\)的质数,设\(dp[i]\)表示当前选的数都是\(i\)的约数且合法 ...
- WSL2 Ubuntu apt-get update失败
情况: 这个问题在github上也有讨论:https://github.com/microsoft/WSL/issues/4342 不过经过我的尝试,是DNS问题,这是默认的配置: 这个配置来自win ...
- Python进阶丨如何创建你的第一个Python元类?
摘要:通过本文,将深入讨论Python元类,其属性,如何以及何时在Python中使用元类. Python元类设置类的行为和规则.元类有助于修改类的实例,并且相当复杂,是Python编程的高级功能之一. ...
- SpringBoot 启动慢的解决办法
项目集成了很多内容,有 700 多个类,IDEA 中启动一次需要 70 秒,非常影响开发效率. 研究问题原因发现有以下几种情况会导致启动速度慢,优化后启动只需 26 秒左右了: 1. 和网卡有关,禁用 ...