Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

let 存在变量提升 #1049

Closed
xmz-dream opened this issue Apr 25, 2021 · 3 comments
Closed

let 存在变量提升 #1049

xmz-dream opened this issue Apr 25, 2021 · 3 comments

Comments

@xmz-dream
Copy link

xmz-dream commented Apr 25, 2021

参考:https://stackoverflow.com/questions/31219420/are-variables-declared-with-let-or-const-hoisted/31222689#31222689

@ruanyf
Copy link
Owner

ruanyf commented Apr 25, 2021

会报错,不能叫做提升吧。

@ruanyf ruanyf closed this as completed Apr 25, 2021
@Liuxingwei
Copy link

Liuxingwei commented May 4, 2022

如果把“定义”和“赋初值”这两个动作同时完成视为提升的话,let 就不存在提升。

如果只把完成“定义”这个动作视为提升的话,let 就存在提升。

浏览器和node.js实现是把这两个动作拆开的。

console.log(a); // ReferenceError: b is not defined

console.log(a); // ReferenceError: Cannot access 'c' before initialization
let a;

MDN 文档认为这也是一种提升:

在 ECMAScript 6 中,let和const同样会被提升变量到代码块的顶部但是不会被赋予初始值。在变量声明之前引用这个变量,将抛出引用错误(ReferenceError)。这个变量将从代码块一开始的时候就处在一个“暂时性死区”,直到这个变量被声明为止。

参见:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Grammar_and_types#%E5%8F%98%E9%87%8F%E7%9A%84%E4%BD%9C%E7%94%A8%E5%9F%9F

@Liuxingwei
Copy link

如果把“定义”和“赋初值”这两个动作同时完成视为提升的话,let 就不存在提升。
如果只把完成“定义”这个动作视为提升的话,let 就存在提升。
浏览器和node.js实现是把这两个动作拆开的。
console.log(a); // ReferenceError: b is not defined
console.log(a); // ReferenceError: Cannot access 'c' before initialization let a;
MDN 文档认为这也是一种提升:

在 ECMAScript 6 中,let和const同样会被提升变量到代码块的顶部但是不会被赋予初始值。在变量声明之前引用这个变量,将抛出引用错误(ReferenceError)。这个变量将从代码块一开始的时候就处在一个“暂时性死区”,直到这个变量被声明为止。

参见:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Grammar_and_types#%E5%8F%98%E9%87%8F%E7%9A%84%E4%BD%9C%E7%94%A8%E5%9F%9F

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants