js-google
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGoogle JavaScript Best Practices
Google JavaScript 最佳实践
Comprehensive JavaScript style and best practices guide based on Google's official JavaScript Style Guide, designed for AI agents and LLMs. Contains 47 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.
基于Google官方JavaScript风格指南的全面JavaScript风格与最佳实践指南,专为AI Agent和LLM设计。包含8个类别下的47条规则,按影响优先级排序,以指导自动化重构和代码生成。
When to Apply
适用场景
Reference these guidelines when:
- Writing new JavaScript or ES6+ code
- Structuring modules and managing imports/exports
- Adding JSDoc type annotations and documentation
- Reviewing code for naming and style consistency
- Refactoring existing JavaScript code
在以下场景参考本指南:
- 编写新的JavaScript或ES6+代码
- 构建模块结构并管理导入/导出
- 添加JSDoc类型注解与文档
- 评审代码的命名与风格一致性
- 重构现有JavaScript代码
Rule Categories by Priority
按优先级划分的规则类别
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Module System & Imports | CRITICAL | |
| 2 | Language Features | CRITICAL | |
| 3 | Type Safety & JSDoc | HIGH | |
| 4 | Naming Conventions | HIGH | |
| 5 | Control Flow & Error Handling | MEDIUM-HIGH | |
| 6 | Functions & Parameters | MEDIUM | |
| 7 | Objects & Arrays | MEDIUM | |
| 8 | Formatting & Style | LOW | |
| 优先级 | 类别 | 影响程度 | 前缀 |
|---|---|---|---|
| 1 | 模块系统与导入 | 关键 | |
| 2 | 语言特性 | 关键 | |
| 3 | 类型安全与JSDoc | 高 | |
| 4 | 命名规范 | 高 | |
| 5 | 控制流与错误处理 | 中高 | |
| 6 | 函数与参数 | 中 | |
| 7 | 对象与数组 | 中 | |
| 8 | 格式与风格 | 低 | |
Quick Reference
快速参考
1. Module System & Imports (CRITICAL)
1. 模块系统与导入(关键)
- - Prevent loading failures from circular imports
module-avoid-circular-dependencies - - Include .js extension in import paths
module-file-extension-in-imports - - Prefer named exports for consistency
module-named-exports-over-default - - Import from same file only once
module-no-duplicate-imports - - Keep original export names
module-no-import-aliasing - - Follow standard file structure order
module-source-file-structure
- - 避免循环导入导致的加载失败
module-avoid-circular-dependencies - - 在导入路径中包含.js扩展名
module-file-extension-in-imports - - 优先使用命名导出以保持一致性
module-named-exports-over-default - - 同一文件仅导入一次
module-no-duplicate-imports - - 保留原始导出名称
module-no-import-aliasing - - 遵循标准文件结构顺序
module-source-file-structure
2. Language Features (CRITICAL)
2. 语言特性(关键)
- - Use const by default, never var
lang-const-over-let-over-var - - Use class syntax over prototype manipulation
lang-es6-classes-over-prototypes - - Always use explicit semicolons
lang-explicit-semicolons - - Never use eval or Function constructor
lang-no-eval - - Never modify built-in prototypes
lang-no-modify-builtins - - Use only standard ECMAScript features
lang-no-non-standard-features - - Never use primitive wrapper objects
lang-no-primitive-wrappers - - Never use the with statement
lang-no-with-statement
- - 默认使用const,绝不使用var
lang-const-over-let-over-var - - 使用类语法而非原型操作
lang-es6-classes-over-prototypes - - 始终使用显式分号
lang-explicit-semicolons - - 绝不使用eval或Function构造函数
lang-no-eval - - 绝不修改内置原型
lang-no-modify-builtins - - 仅使用标准ECMAScript特性
lang-no-non-standard-features - - 绝不使用原始值包装对象
lang-no-primitive-wrappers - - 绝不使用with语句
lang-no-with-statement
3. Type Safety & JSDoc (HIGH)
3. 类型安全与JSDoc(高)
- - Use parentheses for type casts
type-cast-with-parentheses - - Annotate enums with static literal values
type-enum-annotations - - Use explicit nullability modifiers
type-explicit-nullability - - Require JSDoc for exported functions
type-jsdoc-required-for-exports - - Always specify template parameters
type-template-parameters - - Use typedef for complex object types
type-typedef-for-complex-types
- - 使用括号进行类型转换
type-cast-with-parentheses - - 为枚举添加静态字面量值注解
type-enum-annotations - - 使用显式可空性修饰符
type-explicit-nullability - - 导出函数必须添加JSDoc
type-jsdoc-required-for-exports - - 始终指定模板参数
type-template-parameters - - 为复杂对象类型使用typedef
type-typedef-for-complex-types
4. Naming Conventions (HIGH)
4. 命名规范(高)
- - Use CONSTANT_CASE for immutable values
naming-constant-case-for-constants - - Prefer descriptive names over brevity
naming-descriptive-over-brief - - Use lowercase with dashes or underscores
naming-file-naming-conventions - - Use lowerCamelCase for methods and variables
naming-lowercamelcase-for-methods - - Avoid dollar sign prefix in identifiers
naming-no-dollar-prefix - - Use UpperCamelCase for classes
naming-uppercamelcase-for-classes
- - 不可变值使用CONSTANT_CASE命名
naming-constant-case-for-constants - - 优先使用描述性名称而非简短名称
naming-descriptive-over-brief - - 使用小写加短横线或下划线命名文件
naming-file-naming-conventions - - 方法与变量使用lowerCamelCase命名
naming-lowercamelcase-for-methods - - 标识符避免使用美元符号前缀
naming-no-dollar-prefix - - 类使用UpperCamelCase命名
naming-uppercamelcase-for-classes
5. Control Flow & Error Handling (MEDIUM-HIGH)
5. 控制流与错误处理(中高)
- - Document empty catch blocks
control-comment-empty-catch - - Prefer for-of over for-in
control-for-of-over-for-in - - Use strict equality except for null checks
control-strict-equality - - Always include default case in switch
control-switch-default-last - - Always throw Error objects
control-throw-error-objects
- - 为空catch块添加文档说明
control-comment-empty-catch - - 优先使用for-of而非for-in
control-for-of-over-for-in - - 除空值检查外,使用严格相等运算符
control-strict-equality - - switch语句中始终包含default分支
control-switch-default-last - - 始终抛出Error对象
control-throw-error-objects
6. Functions & Parameters (MEDIUM)
6. 函数与参数(中)
- - Prefer arrow functions for nested functions
func-arrow-functions-for-nested - - Always use parentheses around arrow params
func-arrow-parentheses - - Use default parameters instead of conditionals
func-default-parameters - - Use rest parameters over arguments
func-rest-parameters-over-arguments - - Use spread operator instead of apply
func-spread-over-apply
- - 嵌套函数优先使用箭头函数
func-arrow-functions-for-nested - - 箭头函数参数始终使用括号包裹
func-arrow-parentheses - - 使用默认参数而非条件判断
func-default-parameters - - 使用剩余参数而非arguments对象
func-rest-parameters-over-arguments - - 使用扩展运算符而非apply方法
func-spread-over-apply
7. Objects & Arrays (MEDIUM)
7. 对象与数组(中)
- - Use array literals over Array constructor
data-array-literals-over-constructor - - Use destructuring for multiple properties
data-destructuring-for-multiple-values - - Never mix quoted and unquoted keys
data-no-mixing-quoted-unquoted-keys - - Use object literals over constructor
data-object-literals-over-constructor - - Use spread over concat and slice
data-spread-over-concat-slice - - Use trailing commas in multi-line literals
data-trailing-commas
- - 优先使用数组字面量而非Array构造函数
data-array-literals-over-constructor - - 多属性取值使用解构赋值
data-destructuring-for-multiple-values - - 绝不混合使用带引号与不带引号的对象键名
data-no-mixing-quoted-unquoted-keys - - 优先使用对象字面量而非构造函数
data-object-literals-over-constructor - - 使用扩展运算符而非concat和slice方法
data-spread-over-concat-slice - - 多行字面量中使用 trailing commas
data-trailing-commas
8. Formatting & Style (LOW)
8. 格式与风格(低)
- - Always use braces for control structures
format-braces-required - - Limit lines to 80 characters
format-column-limit - - Place one statement per line
format-one-statement-per-line - - Use single quotes for string literals
format-single-quotes - - Use two-space indentation
format-two-space-indent
- - 控制结构始终使用大括号
format-braces-required - - 每行代码限制为80个字符
format-column-limit - - 每行仅包含一条语句
format-one-statement-per-line - - 字符串字面量使用单引号
format-single-quotes - - 使用两个空格缩进
format-two-space-indent
How to Use
使用方法
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
阅读单个参考文件获取详细说明与代码示例:
- Section definitions - 类别结构与影响程度说明
- Rule template - 添加新规则的模板
Full Compiled Document
完整编译文档
For a complete compiled guide with all rules, see AGENTS.md.
如需包含所有规则的完整指南,请查看AGENTS.md。
Reference Files
参考文件
| File | Description |
|---|---|
| AGENTS.md | Complete compiled guide with all rules |
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |
| metadata.json | Version and reference information |
| 文件 | 描述 |
|---|---|
| AGENTS.md | 包含所有规则的完整编译指南 |
| references/_sections.md | 类别定义与排序 |
| assets/templates/_template.md | 新规则模板 |
| metadata.json | 版本与参考信息 |