gpt4 book ai didi

angular - tslint no-var-keyword - "forbidden ' var' 关键字的目的是什么?

转载 作者:行者123 更新时间:2023-12-04 12:11:53 25 4
gpt4 key购买 nike

tslint no-var-keyword(“禁止的‘var’关键字”)的目的是什么? tslint 似乎在我的代码中每次出现 var 关键字时都会记录一个错误。 tslint 是否说明应在 ng2 中无条件排除 var 关键字?如果是,那为什么?

最佳答案

该规则及其基本原理记录在此处 https://palantir.github.io/tslint/rules/no-var-keyword/

Declaring variables using var has several edge case behaviors that make var unsuitable for modern code. Variables declared by var have their parent function block as their scope, ignoring other control flow statements. vars have declaration “hoisting” (similar to functions) and can appear to be used before declaration.

Variables declared by const and let instead have as their scope the block in which they are defined, and are not allowed to used before declaration or be re-declared with another const or let.


针对您的第二个问题,这与 Angular、ng2 或任何其他版本无关。这是一个 typescript 问题。是的,他们正试图让你放弃 var总之,赞成 letconst .
let 和 var 的区别在于作用域。您必须在使用前进行声明,并且可以在嵌套范围内重新声明。在 TS 中,您也可以借此机会声明不同的类型,尽管我不认为这是一种编程风格。

关于angular - tslint no-var-keyword - "forbidden ' var' 关键字的目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46699317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com