Code Monkey home page Code Monkey logo

Comments (2)

huihongzhou avatar huihongzhou commented on July 30, 2024 2

let number = 1;
switch(number){
case 1:
{ let name = 'Jony';}
default:
console.log(name)
}

from blog.

toFrankie avatar toFrankie commented on July 30, 2024
  1. switch 语句的 case 内若有变量声明,必须要用大括号括起来。其中 ESLint 中有个 no-case-declarations 规则专门检查这种情况。

  2. 其实 letconstclass 也是会“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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.