Typescript 中的 var 和 let 有什么区别? [英] What is the difference between var and let in Typescript?

查看:32
本文介绍了Typescript 中的 var 和 let 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提交了一个关于堆栈溢出的问题,询问如果之前的版本已经在执行,我该如何阻止 putTestQuestionResponses() 函数的执行.

I submitted a question on stack overflow asking how I could stop the putTestQuestionResponses() function from executing IF a previous version was already executing.

回复是在此代码的第 2 行中添加一个处理标志.

The reply was to add in a processing flag which is here on line 2 of this code.

你能告诉我为什么在这里使用let"而不是var"吗?

Can you tell me why use a "let" instead of a "var" here?

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);
}]

推荐答案

var 声明是函数范围的,而 let 声明是块范围的.

var declaration is function scoped and let declaration is block scoped.

参见 https://basarat.gitbooks.io/typescript/content/docs/let.html 了解更多详情.

See https://basarat.gitbooks.io/typescript/content/docs/let.html for more details.

这篇关于Typescript 中的 var 和 let 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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