为什么JavaScript提升变量? [英] Why does JavaScript hoist variables?

查看:123
本文介绍了为什么JavaScript提升变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么JavaScript提升变量?

设计师决定实施吊装时的理由是什么?
是否有其他流行语言可以做到这一点?

What was the rationale of the designers when they decided to implement hoisting? Are there any other popular languages that do this?

请提供文档和/或记录的相关链接。

Please provide relevant links to documentation and/or records.

推荐答案

正如Stoyan Stefanov在JavaScript Patterns一书中解释的那样,提升是JavaScript解释器实现的结果。

As Stoyan Stefanov explains in "JavaScript Patterns" book, the hoisting is result of JavaScript interpreter implementation.

JS代码解释分两次执行。在第一次传递期间,解释器处理变量和函数声明。

The JS code interpretation performed in two passes. During the first pass, the interpreter processes variable and function declarations.

第二遍实际上是代码执行步骤。解释器处理函数表达式和未声明的变量。

The second pass is actually code execution step. The interpreter processes function expressions and undeclared variables.

因此,我们可以使用提升概念来描述这种行为。

Thus, we can use the "hoisting" concept to describe such behavior.

希望这会有所帮助。

这篇关于为什么JavaScript提升变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆