javascript_javascript吊装

javascript

Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution.

提升是一种JavaScript机制,在执行代码之前,变量和函数声明会移至其作用域的顶部。

Hoisting is JavaScript’s default behavior of moving declarations to the top.

提升是JavaScript将声明移到顶部的默认行为。

悬挂了JavaScript声明 (JavaScript Declarations are Hoisted)

In JavaScript, a variable can be declared after it has been used. In other words; a variable can be used before it has been declared.

在JavaScript中,变量可以在使用后声明。 换一种说法; 变量可以在声明之前使用。

Image for post
result => 5
结果=> 5

To understand this, you have to understand the term “hoisting”.

要了解这一点,您必须了解术语“吊装”。

Hoisting is JavaScript’s default behavior of moving all declarations to the top of the current scope (to the top of the current script or the current function).

提升是JavaScript的默认行为,即将所有声明移到当前作用域的顶部(当前脚本或当前函数的顶部)。

let和const关键字 (The let and const Keywords)

In JavaScript, a ReferenceError is thrown when trying to access a previously undeclared variable.

在JavaScript中,尝试访问以前未声明的变量时将抛出ReferenceError。

Variables defined with let and const are hoisted to the top of the block, but not initialized.

letconst定义的变量被提升到块的顶部,但是没有初始化

Meaning: The block of code is aware of the variable, but it cannot be used until it has been declared.

含义:代码块知道该变量,但是在声明它之前不能使用它。

Using a let or const variable before it is declared will result in a ReferenceError.

在声明之前使用letconst变量将导致ReferenceError

The variable is in a “temporal dead zone” from the start of the block until it is declared:

从块开始到声明为止,该变量位于“临时死区”中:

Image for post

不初始化JavaScript初始化 (JavaScript Initializations are Not Hoisted)

JavaScript only hoists declarations, not initializations.

JavaScript仅提升声明,而不提升初始化。

Example 1 does not give the same result as Example 2:

实施例1 没有给出相同的结果与实施例2:

Image for post
Result => 5 7
结果=> 5 7

变种 (var)

The scope of a variable declared with the keyword var is its current execution context. This is either the enclosing function or for variables declared outside any function, global.

用关键字var声明的变量的范围是其当前的执行上下文 。 这是封闭函数,也可以是在任何函数之外声明的变量global

Image for post
Result => x is 5 and y is undefined
结果=> x为5并且y未定义

This is what really happened here.

这就是这里真正发生的事情。

Does it make sense that y is undefined in the last example?

在上一个示例中y未定义是否有意义?

This is because only the declaration (var y), not the initialization (=7) is hoisted to the top.

这是因为只有声明(var y)而不是初始化(= 7)放在顶部。

Because of hoisting, y has been declared before it is used, but because initialization are not hoisted, the value of y is undefined.

由于提升,已在使用y之前声明了y,但由于未提升初始化,因此y的值未定义。

Example 2 is the same as writing:

示例2与写作相同:

Image for post
Result => 5 undefined
结果=> 5个未定义

在顶部声明变量! (Declare Your Variables At the Top!)

Hoisting is (to many developers) an unknown or overlooked behavior of JavaScript.

对于许多开发人员而言,吊装是JavaScript的未知或被忽略的行为。

If a developer doesn’t understand hoisting, programs may contain bugs (errors).

如果开发人员不理解吊装,程序可能包含错误(错误)。

To avoid bugs, always declare all variables at the beginning of every scope.

为避免错误,请始终在每个作用域的开头声明所有变量。

Since this is how JavaScript interprets the code, it is always a good rule.

由于这是JavaScript解释代码的方式,因此始终是一个好规则。

翻译自: https://medium.com/@sekou.dosso82_17502/javascript-hoisting-e72497ec8157

javascript

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值