在 TypeScript(或 Javascript)中 const 与 let 的好处 [英] Benefit of const vs let in TypeScript (or Javascript)

查看:29
本文介绍了在 TypeScript(或 Javascript)中 const 与 let 的好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 TypeScript Deep Dive 并且我看到两者letconst 是块作用域,这很好.显然,const 不能改变(它是不可变的).但是为什么 ReSharper 鼓励我尽可能多地将 let 更改为 const ?我假设 ReSharper 认为使用 const 比使用 let 有性能提升?constlet 之间有速度差异吗?使用 const 有什么不同的原因吗?以下面的例子:

I was reading the TypeScript Deep Dive and I see that both let and const are block scoped, which is great. Obviously, a const cannot be changed (it is immutable). But why is ReSharper is encouraging me to change as many lets to const as I can? I'm assuming ReSharper thinks there is a performance gain in using const over let? Is there a speed difference between const and let? Is there a different reason to use const? Take the following example:

for (let i = 0; i < this.InputControl.ParentQuestions.length; i++) {
    const id = this.InputControl.ParentQuestions[i].ParentId;
    const value = this.InputControl.ParentQuestions[i].ParentRequiredAnswer;
    if (!formVals[id]) return false;
    if (formVals[id].toLowerCase() != value.toLowerCase()) return false;
}

以前,我有 let idlet value,但 ReSharper 要求我将它们更改为 const,这有效,但为什么会这样在这种情况下更好?或者无论如何?

Previously, I had let id and let value but ReSharper asked me to change them to const, which works, but why is it better in this case? Or in any case?

我还发现了 关于 SO 的这个问题,但它更多地讨论了 letconst 的作用,而不是为什么一个比另一个更好.它确实说要尽可能多地使用 const,但这有什么好处?

I also found this question on SO, but it talks more about what let and const do, not so much why one is better than the other. It does say to use const as much as possible, but what benefit does that provide?

推荐答案

我同意 Giorgi 的观点主要原因.代码分析器也可以确定用 let 声明的变量不会被重新分配,并像使用 const 声明它一样优化它.(见鬼,linter 有规则来检测这个并建议使用 const 而不是 let.)

I agree with Giorgi that performance is not the main reason. A code analyzer could just as well determine that a variable declared with let is not ever reassigned and optimize it the same as if you had declared it with const. (Heck, linters have rules to detect this and suggest using const instead of let.)

是的,它确实向读者发出信号,表明您不会分配给变量.const 的好处,而不是发表评论说同样的事情,主要是 const 是一种标准的信号方式.作为标准,它比自定义注释更容易传输信息.(此外,注释可能是错误的,但 const 不会让你出错.)不过,我认为这不是主要的好处.

Yes, it does signal to the reader that you're not going to assign to the variable. The benefit of const, over putting a comment saying the same thing, is mainly that const is a standard way of signalling it. Being standard, it transmits the information more readily than custom comments. (Also, a comment could be wrong but const won't let you be wrong.) I don't think this is the main benefit though.

最小权限原则"经常与const一起调用,但我为什么要关心最小权限呢?因为它有助于及早发现编码错误.考虑以下代码:

The "principle of least privilege" is often invoked in conjunction with const, but why should I care about the least privilege? Because it helps with early detection of coding mistakes. Consider the following code:

function findSomethingInDocument(document) {
    let tree = getTreeFromDocument(document); // We get some sort of tree structure.
    let found;
    for (const child of tree.children) {
        if (child.someFlag) {
            tree = child; // Oops I meant found = child :(
            break;
        }
    }
    return found;
}

在这段代码中,当我想输入 found = child 时,我输入了 tree = child.是的,该错误可以在测试中发现.但是为什么要等待测试呢?我从来没有想过改变 tree.如果我将其标记为 const,那么我会立即了解到错误,因为编译器会通知我.我不必等待测试.上面的代码相当简单,但想象一个使用更多变量的更复杂的算法.

In this code, I typed tree = child when I meant to type found = child. Yes, the bug can be found in testing. But why wait for testing? I never meant tree to be changed. If I had marked it const then I would have immediately learned the error because the compiler would informed me of it. I would not have to wait for testing. The code above is fairly simple but imagine a more complicated algorithm that uses more variables.

这篇关于在 TypeScript(或 Javascript)中 const 与 let 的好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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