Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

let const 选择问题 #108

Open
nmsn opened this issue Jan 18, 2024 · 1 comment
Open

let const 选择问题 #108

nmsn opened this issue Jan 18, 2024 · 1 comment
Labels

Comments

@nmsn
Copy link
Owner

nmsn commented Jan 18, 2024

No description provided.

@nmsn nmsn added the ES6 label Jan 18, 2024
@nmsn
Copy link
Owner Author

nmsn commented Jan 18, 2024

参考:https://www.cnblogs.com/ziyunfei/p/6038213.html

两种主流观点

  • 默认全用 let,只在符合判断条件时用 const
  • 默认全用 let,只有变量需要被重新赋值时使用 let

总体来说:

  • 默认全用 let 的方法非常主观,不容易判断使用 const 的条件,因此容易在团队中统一代码风格
  • 默认全用 const 的方法非常客观,判断简单,容易统一代码风格

并且使用默认 const 方案还有几个优点:

  • 保证变量不会轻易被赋值,会迫使写代码的人创建一个新的变量名
var ids = document.getElementById("ids").value // 多个 id 组成的字符串,比如 "100 101 102"
ids = ids.split(" ") // 拆分成数组
ids.forEach(...
  • 配合 no-const-assign 能够避免全局替换错误(文章中的示例)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant