Comments (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.
开了下脑洞,知识串起来就不难知道面试官的意图了
from fe9-interview.
Related Issues (20)
- 微店: 将二维数组转成一维数组的方法有哪些(一面) HOT 1
- 有赞: 为什么在类实例中可以访问this.setState (二面) HOT 1
- 兑吧: prototype 和 proto 区别(一面) HOT 1
- 今日头条: 介绍下Promise,内部实现(一面) HOT 1
- 有赞: 在不改变原数组的前提下,添加或删除某个元素(一面) HOT 2
- 阿里巴巴: 使用伪码实现 npm install 部分内容(二面)
- 有赞: sum(2, 3)实现sum(2)(3)的效果(一面) HOT 1
- To Tom0121 前端开发中设计到什么模式 HOT 1
- 滴滴: JS异步解决方案的发展历程以及优缺点(一面) HOT 1
- 携程:数组去重和数组扁平化(一面) HOT 1
- 阿里巴巴:实现一些主流框架的循环渲染(笔试) HOT 1
- To littleKnave: 实现一个Promise.prototype.first(今日头条) HOT 1
- 阿里巴巴:浏览器的强缓存和协商缓存(一面) HOT 3
- 51 信用卡: apply,call,bind 的区别(一面) HOT 2
- 为什么使用 ctrl+f5 或启用开发者面板中 Disable cache 选项后还会存在读取缓存的现象 HOT 1
- 阿里:Koa-body原理(一面)
- 爱云校:算法两道(一、1-100所有的素数 二、快速排序)(笔试) HOT 2
- 腾讯:js中精度问题及解决方案(一面) HOT 1
- 如何将 node 标准的回调 API 在 fibjs 内转为"同步"调用形式(一面) HOT 1
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 fe9-interview.