在TypeScript中,什么时候使用"let"?以及何时使用"const"? [英] In TypeScript, when do you use "let" and when do you use "const"?
问题描述
在TypeScript中,何时使用"let"以及何时使用"const"?
const
代表 constant
,这意味着以后不能重新分配该变量./p>
let
与 var
类似,除了它是块作用域的,这意味着它可以在 for
循环内声明,并且可以对于该 for
循环的主体而言是本地的(因此在其外部不存在)
后者不同于 var
变量,后者可以在任何地方声明,但始终在函数作用域内是本地的.
通常,最好尝试将变量尽可能地定义为 const
.
In TypeScript, when do you use "let" and when do you use "const"?
const
stands for constant
, and it means the variable cannot be reassigned at a later time.
let
is similar to var
except that it is block scoped, which means it can be declared inside of a for
loop and will be local to the body of that for
loop (and therefor does not exist outside of it)
The latter is different from a var
variable which can be declared anywhere but is always local to the function scope.
In general it is good practice to try and define your variables as const
as much as possible.
这篇关于在TypeScript中,什么时候使用"let"?以及何时使用"const"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!