简体   繁体   English

在TypeScript(或Javascript)中使用const vs let的好处

[英]Benefit of const vs let in TypeScript (or Javascript)

I was reading the TypeScript Deep Dive and I see that both let and const are block scoped, which is great. 我正在阅读TypeScript Deep Dive ,我发现letconst都是块作用域,这很棒。 Obviously, a const cannot be changed (it is immutable). 显然,const不能改变(它是不可变的)。 But why is ReSharper is encouraging me to change as many let s to const as I can? 但是为什么ReSharper鼓励我改变尽可能多的let我变成const呢? I'm assuming ReSharper thinks there is a performance gain in using const over let ? 我假设ReSharper认为使用const over let会有性能提升吗? Is there a speed difference between const and let ? constlet之间有速度差吗? Is there a different reason to use const ? 使用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;
}

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? 以前,我已经let idlet value但是ReSharper让我把它们改成const ,这有效,但为什么在这种情况下更好呢? Or in any case? 或者无论如何?

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. 我也在SO上发现了这个问题 ,但它更多地讨论了letconst作用,而不是为什么一个比另一个好。 It does say to use const as much as possible, but what benefit does that provide? 它确实说尽可能使用const ,但它提供了什么好处?

I agree with Giorgi that performance is not the main reason. 我同意Giorgi的说法,表现不是主要原因。 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 . 代码分析器也可以确定用let声明的变量不会被重新分配,并且就像使用const声明它一样优化它。 (Heck, linters have rules to detect this and suggest using const instead of let .) (哎呀,linters有规则来检测这个并建议使用const而不是let 。)

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. const的好处,而不是发表评论说同样的事情,主要是const是一种发信号的标准方式。 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? “最小特权原则”通常与const一起调用,但为什么我应该关心最小特权呢? 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;
}

In this code, I typed tree = child when I meant to type found = child . 在这段代码中,当我打算输入found = child时,我输入了tree = child found = child Yes, the bug can be found in testing. 是的, 可以在测试中找到错误。 But why wait for testing? 但为什么要等待测试? I never meant tree to be changed. 我从没想过要改变tree If I had marked it const then I would have immediately learned the error because the compiler would informed me of it. 如果我将它标记为const那么我会立即学习错误,因为编译器会告诉我它。 I would not have to wait for testing. 我不必等待测试。 The code above is fairly simple but imagine a more complicated algorithm that uses more variables. 上面的代码相当简单,但想象一下使用更多变量的更复杂的算法。

When you have a variable which can be declared as const , and you declare it as such you inform the reader that you don't plan to reassign it to a different value later on. 如果您有一个可以声明为const的变量,并且您将其声明为此类,则通知读者您不打算稍后将其重新分配给其他值。

Also by declaring a variable const it means you have thought up front that you don't plan to reassign it, which in turn can protect you from accidental bugs or errors. 同样通过声明变量const这意味着您已经预先考虑过您不打算重新分配它,这反过来可以保护您免受意外错误或错误的影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM