跳到正文
Skillpedia
网站与界面(前端)75k · #2781

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。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/const-let/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 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.

repo: thedaviddias/Front-End-Checklist

热度

GitHub 星标
75k
排名
#2781
许可证
MIT
更新时间
2026/10/31

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能