Typescript 中的 var 和 let 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35572895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between var and let in Typescript?
提问by Alan2
I submitted a question on stack overflow asking how I could stop the putTestQuestionResponses() function from executing IF a previous version was already executing.
我提交了一个关于堆栈溢出的问题,询问如果以前的版本已经在执行,我该如何阻止 putTestQuestionResponses() 函数的执行。
The reply was to add in a processing flag which is here on line 2 of this code.
答复是添加一个处理标志,该标志位于此代码的第 2 行。
Can you tell me why use a "let" instead of a "var" here?
你能告诉我为什么在这里使用“let”而不是“var”吗?
var promisePutTestQuestion;
let processing = false;
onEnter: ['$interval', 'questionService',
($interval, qus: IQuestionService) => {
promisePutTestQuestion = $interval(() => {
if (processing)
return;
processing = true;
qus.putTestQuestionResponses()
.then(() => processing = false)
}, 5 * 1000);
}],
onExit: ['$interval', ($interval) => {
$interval.cancel(promisePutTestQuestion);
}]
回答by Martin Vseticka
var
declaration is function scoped and let
declaration is block scoped.
var
声明是函数范围的,let
声明是块范围的。
See https://basarat.gitbooks.io/typescript/content/docs/let.htmlfor more details.
有关更多详细信息,请参阅https://basarat.gitbooks.io/typescript/content/docs/let.html。
回答by wyl
example:
例子:
// demo: var
for(var i =0 ; i<5 ; i++){
console.log(i)
}//finally i =5
console.log(i) // i=5
// demo: let
for(let i = 0; i<5;i++){
console.log(i)
}
console.log(i)// i is undefined
it is easy to know
很容易知道
回答by chenchu kotari
var
variables in JavaScript are function scoped. This is different from many other languages (C#, Java, etc.) where the variables are block scoped. If you bring a block scoped mindset to JavaScript, you would expect the following to print 123, instead it will print 456:
var
JavaScript 中的变量是函数作用域的。这与变量是块作用域的许多其他语言(C#、Java 等)不同。如果您将块范围的思维方式引入 JavaScript,您会期望以下内容打印 123,而不是打印 456:
var foo = 123;
if (true) {
var foo = 456;
}
console.log(foo); // 456
This is because {
does not create a new variable scope. The variable foo
is the same inside the if block as it is outside the if block. This is a common source of errors in JavaScript programming. This is why TypeScript (and ES6) introduces the let
keyword to allow you to define variables with true block scope. That is, if you use let
instead of var
, you get a true unique element disconnected from what you might have defined outside the scope. The same example is demonstrated with let
:
这是因为{
不会创建新的变量作用域。foo
if 块内部的变量与 if 块外部的变量相同。这是 JavaScript 编程中常见的错误来源。这就是 TypeScript(和 ES6)引入let
关键字以允许您定义具有真正块作用域的变量的原因。也就是说,如果您使用let
而不是var
,您会得到一个真正的唯一元素,与您可能在范围外定义的内容断开连接。使用以下示例演示了相同的示例let
:
let foo = 123;
if (true) {
let foo = 456;
}
console.log(foo); // 123
回答by Dharmendra Prajapati
function varTest() {
var x = 1;
if (true) {
var x = 2; // same variable!
console.log(x); // 2
}
console.log(x); // 2
}
function letTest() {
let x = 1;
if (true) {
let x = 2; // different variable
console.log(x); // 2
}
console.log(x); // 1
}
I found this here
我在这里找到了这个
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let