Giter Club home page Giter Club logo

Comments (2)

yingpengsha avatar yingpengsha commented on July 28, 2024 2

答案:

只要块级作用域内存在let、const命令,它所声明的变量就“绑定”(binding)这个区域,不再受外部的影响。

其本质就是,只要进入当前作用域,所要使用的变量就已经存在,但是不可获取,只有等到声明变量的那一行代码出现,才可以获取和使用该变量。

ES6 规定暂时性死区和 let 、const语句不出现变量提升,主要是为了减少运行时的错误,防止在变量声明前就是用这个变量,从而导致意料之外的行为

解答思路:

虽然面试官只问了 暂时性死区 一个概念,但其实考核的内容包括let、const、变量提升、作用域链等内容。

首先导致暂时性死区出现,是因为作用域内出现了 let 和 const 命令。
与之相反的 var 命令并不会出现暂时性死区的现象。

以下举例说明

let

var num = 1;
if (true) {
  num = 'a'; // Error: Uncaught ReferenceError: num is not defined
  let num;
}

因为在 if 中声明了一个局部变量 num , 导致暂时性死区,if 里的 num 则与这个块级作用域绑定,不再受全局变量 num 影响,同时 let 不存在变量提升,所以在 let 前赋值的 num 是非法的。const与之同理

相关内容

from fe9-interview.

johanazhu avatar johanazhu commented on July 28, 2024

image

开了下脑洞,知识串起来就不难知道面试官的意图了

from fe9-interview.

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.