const-let
它能做什么
这项技能检查 JavaScript 中的 var 声明,并建议改用 const 或 let。适合希望代码更安全、更易读的开发者。
适用场景
- ✓当你在较旧的 JavaScript 文件中发现 var 时。
- ✓当某个变量似乎在不应被访问的地方可以访问时。
- ✓当你在发布前为了清晰度而审查代码时。
技能链接
https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/const-let/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请逐一检查这段脚本,把每个 var 替换为 const 或 let。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会列出每个 var 声明及其行号,对永不改变的值使用 const,对会被重新赋值的值使用 let。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Prefer const and let over var
var's function scope and hoisting behavior regularly causes bugs where variables are accessible outside the block they were declared in. const and let enforce block scope and make code intent clearer — const signals that a binding should not be reassigned, which helps readers understand data flow.
Quick Reference
- Use const for values that are never reassigned
- Use let when reassignment is needed
- Never use var — it has function scope and is hoisted, causing subtle bugs
- const does not mean immutable — objects and arrays declared with const can still be mutated
Check
Scan this JavaScript file for any use of var and flag every occurrence with line numbers.
Fix
Replace all var declarations with const or let as appropriate. Use const when the variable is never reassigned, let otherwise.
Explain
Explain why const and let are preferred over var, covering scope, hoisting, and temporal dead zone.
Code Review
Review scripts, client components, and browser execution paths related to Prefer const and let over var. Flag exact imports, event handlers, runtime side effects, or blocking operations that violate the rule, and state how the change should be verified in the browser.
For full implementation details, code examples, and framework-specific guidance,
see references/rule.md.
Rule page: https://frontendchecklist.io/rules/javascript/const-let
常见问题
var 为什么有风险?
这项技能说明,var 具有函数作用域和变量提升的特性,可能导致隐蔽的错误。
什么时候应该使用 const?
当变量不会被重新赋值时。
const 会让对象不可修改吗?
不会。这项技能指出,用 const 声明的对象和数组仍然可以被修改。
详情
作者的原始描述
Use when reviewing scripts, client components, bundles, or runtime behavior related to Prefer const and let over var. Inspect both source code and the browser execution path so fixes target the real bottleneck or bug.
热度
- GitHub 星标
- 75k
- 排名
- #2781
- 许可证
- MIT
- 更新时间
- 2026/10/31
评论
- …
同一仓库的更多技能
frontendchecklist
75k使用命令行工具,依据 380 多条关于无障碍、速度、SEO 和安全的规则,检查前端代码和线上页面。
frontend-checklist-global
75k依据全部 386 条 Front-End Checklist 规则审查前端项目,并为发现的每个问题给出具体修复方法。
404-page
75k检查你的页面缺失提示是否提供有用的链接,并返回真正的 404 状态,帮助迷路的访客找到方向。
about-page
75k审查你的网站是否有清晰的关于页面,说明由谁运营,以建立访客和搜索引擎的信任。