Comments (2)
let number = 1;
switch(number){
case 1:
{ let name = 'Jony';}
default:
console.log(name)
}
from blog.
-
在
switch
语句的case
内若有变量声明,必须要用大括号括起来。其中 ESLint 中有个 no-case-declarations 规则专门检查这种情况。 -
其实
let
、const
、class
也是会“Hoisting”的(可断点看执行上下文)。只是 TDZ 机制,使得它在定义之前调用抛出错误而已。但如果从使用角度看,也可以认为是不会提升的。
let number = 2
switch (number) {
case 1:
let name = 'jony'
break
case 2:
name = 'yu'
break
}
这个示例,在不同浏览器或同一浏览器不同版本,抛出的提示是有区别的:
- ReferenceError: Cannot access uninitialized variable.
- Uncaught ReferenceError: name is not defined
- ReferenceError: can't access lexical declaration 'name' before initialization
从这些提示信息看,各 JS 引擎底层实现,细微之处应该还是有区别的。但它们是都遵循 TDZ 的要求(先定义再使用)。
例如,那个 async/await + promise
执行顺序的问题,在不同引擎下,在今天仍然是有区别的,比如它们处理 Promise.resolve()
的方式,在引擎底层是有区别的,导致了不同的结果。
from blog.
Related Issues (20)
- Typescript中的内置类型以及内置类型的实现原理
- 聊一聊Typescript中与this相关的类型定义
- 转载申请 HOT 1
- 深入的聊一聊Typescript中的module和namespace
- 深入浅出浏览器中的ES modules
- Typescript代码整洁之道 HOT 1
- Node server typescript改造指南
- 深入浅出前端微服务及其实现原理 HOT 1
- skypack原理,如何快速实现comonjs到esm的转化
- 深入浅出svelte.js
- js打包时间缩短90%,bundleless生产环境实践总结 HOT 2
- 深入浅出pixi.js
- pixi.js源码阅读
- 深入浅出fabricjs
- 优雅的阅读fabricjs的源码
- 深入聊一聊__esModule HOT 1
- 深入谈谈React18.x
- 深入聊聊package.json HOT 4
- 深入浅出Nodejs中的大文件读写
- 如何优雅的使用tailwindcss
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blog.