JavaScript 错误
50 个错误
JavaScript 常见错误排查手册:TypeError、作用域陷阱、异步竞态等高频问题的成因与修复。
50 / 50
- 1
ReferenceError: x 是不 定义
你 referenced 变量 name 那个 JavaScript doesn't know 关于 — 它是 从不 declared 使用 `let`, `const`, or `var` in 任何 accessible 作用域.
- 2
TypeError: x 是不 函数
你 used parentheses 到 call 值, 但那个值 是不 函数 — 它's 数字, 字符串, 对象, or `undefined`.
- 3
TypeError: Cannot 读取属性的 undefined
你 used 点 notation on 值那个是 `undefined` — 属性 doesn't exist 因为对象它自己是 missing.
- 4
TypeError: Cannot 读取属性的 空
你 used 点 notation on 值那个是 `空` — `空` 有否 属性, 所以 access 失败.
- 5
SyntaxError: Unexpected 令牌
JavaScript 找到 symbol 哪里它 shouldn't 是 — 通常 missing bracket, comma, or quote earlier in code.
- 6
ReferenceError: Cannot access 'x' 之前 initialization
你 used `let` or `const` 变量之前它 是 declared — JavaScript knows 它 exists (hoisting) 但 won't let 你 access 它 yet.
- 7
NaN Result
你的 calculation resulted in `NaN` — 通常因为你 mixed 数字使用 `undefined`, 非-numeric 字符串, or other 无效值.
- 8
Promise 不 Awaited
你 called 异步函数不使用 `等待`, 所以你 获取 Promise 对象相反的 实际 result 你 expected.
- 9
RangeError: 最大值 call stack 大小 exceeded
你的 code 有无限 or excessive recursion — 函数 calls 它自己不使用 proper 基础情况, filling 上 call stack 直到它 overflows.
- 10
SyntaxError: Missing parenthesis 之后实参列表
你 called 函数但 forgot 到关闭 parentheses, 所以 JavaScript cannot parse call.
- 11
TypeError: Cannot 读取属性的 undefined (读取 '长度')
你 accessed .长度 on 某事那个是 undefined — 通常变量那个是 从不 assigned or lookup 那个 returned undefined.
- 12
RangeError: 无效数组长度
你 tried 到创建 or resize 数组使用无效长度 — 负, 非-integer, or 也大.
- 13
SyntaxError: Identifier 'x' 有 already 是 declared
你 declared 变量使用相同 name 两次 in 相同作用域 using let or const — JavaScript 做不 允许这个.
- 14
TypeError: x 是不 iterable
你 tried 到 iterate over 某事那个是 不 iterable — for...的 and spread 仅工作 on 数组, 字符串, 映射, 设置, and other iterables.
- 15
UnhandledPromiseRejection
Promise 是拒绝 (失败) 但你 做不 provide 错误处理函数 — rejection goes unhandled, 哪个可能 crash app.
- 16
SyntaxError: Unexpected 端的 输入
你的 code 有 unclosed bracket, brace, parenthesis, or template literal — JavaScript ran out 的输入虽然 still parsing.
- 17
TypeError: Cannot assign 到读取仅 属性
你 tried 到 assign 值到 属性那个是 读取-仅 — 对象是 frozen or 属性是 定义 as 非-writable.
- 18
RangeError: 无效 code 点
你 called 字符串.fromCodePoint() 使用值 那个是 不有效 Unicode code 点 — 它必须是 非-负 integer 上到 0x10FFFF.
- 19
SyntaxError: Unexpected 令牌 in JSON
你 called JSON.parse() on 字符串那个做 不 conform 到 JSON 语法 — JSON 需要双-quoted 键 and 字符串值, 否 trailing commas, 否 comments.
- 20
TypeError: Converting circular 结构到 JSON
你的对象包含 circular 引用 (属性那个点 后面到 对象它自己), 哪个 JSON.stringify cannot handle 因为它 会循环 forever.
- 21
URIError: URI malformed
你通过 malformed URI 字符串到 decodeURIComponent() — 它包含无效百分比-encoded 序列.
- 22
SyntaxError: Duplicate 参数 name 不允许 in 这个上下文
你 declared 函数使用 duplicate 参数 names 虽然 in strict 模式 — 这个是 不允许.
- 23
TypeError: Promise resolver 是不 函数
你 called 新 Promise() 使用实参那个是 不函数 — Promise constructor expects executor 函数 (resolve, 拒绝) => {}.
- 24
TypeError: Assignment 到常量变量
你 declared 变量使用 const and 然后 tried 到 assign 新值 到它 — const 变量 cannot 是 reassigned.
- 25
RangeError: 无效 Date
你通过无效值 到 Date constructor, resulting in 无效 Date 对象那个返回 NaN for 所有方法.
- 26
DOMException: Blocked frame 使用原点从 accessing cross-原点 frame
你 tried 到 access iframe, window, or document 从不同原点 — 相同-原点 Policy 块这个 for security.
- 27
SyntaxError: Unexpected 令牌 < in JSON
你 called JSON.parse() on 响应那个是 HTML, 不 JSON — 通常 API 错误 page or redirect 到登录 page.
- 28
TypeError: Reduce 的空 数组使用否 初始值
你 called 数组.reduce() on 空数组不使用其次实参 (初始值) — reduce 有无 到开始使用.
- 29
TypeError: Cannot 读取属性的 空 (读取 'addEventListener')
你 tried 到添加事件监听器到 元素那个做 不 exist in DOM — selector returned 空.
- 30
SyntaxError: Illegal 返回语句
你 used `返回` 之外的 函数请求体 — 返回是 仅有效内部函数.
- 31
TypeError: x.replace 不是函数
你对数字、数组或对象调用了 .replace(或 .trim、.split)——这些方法只存在于字符串上。
- 32
RangeError: 无效的 repeat 次数
你向 .repeat 传入了负数、Infinity 或过大的次数——结果无法构成合法字符串。
- 33
TypeError: 不能混合 BigInt 与其他类型
你把 BigInt 与 Number 相加或比较——JavaScript 不会隐式转换,运算失败。
- 34
TypeError: 用作 WeakMap 键的值无效
你用字符串键调用了 weakMap.set('key', value)——WeakMap 只接受对象作为键。
- 35
TypeError: 对象不可迭代
你对普通对象用了 {...obj} 或 for...of——对象默认不可迭代。
- 36
RangeError: flat 深度过大
你向 .flat 传入了极大深度(或 Infinity)——递归展平耗尽栈空间。
- 37
RangeError: toFixed() 位数参数必须在 0 到 100 之间
你向 .toFixed 传入了负或过大的精度——只允许 0–100。
- 38
SyntaxError: 箭头后出现意外换行
你把 => 写在一行、函数体写在下一行——解析器认为箭头没有函数体。
- 39
SyntaxError: 无效的简写属性初始化器
你在对象字面量中写了 { 1+1 } 或 { foo() } 期望简写,但简写只接受标识符。
- 40
SyntaxError: 可选链不能与 new 同时使用
你写了 new?.Foo()——解析器拒绝,因为 new 与 ?. 不能组合。
- 41
SyntaxError: 未定义的标签
你写了 break outerLoop,但没有名为 outerLoop 的标签包围该循环——目标不存在。
- 42
SyntaxError: 声明之前使用 export
你在声明之前在 export 语句中引用了某名称——模块无法解析该绑定。
- 43
SyntaxError: 类体中标识符已声明
你在类中定义了两个同名方法——解析器拒绝重复。
- 44
Array.sort 默认按字典序排序
你对数字数组调用 .sort() 未传比较器——它按字符串排序,[10, 2, 1] 变成 [1, 10, 2]。
- 45
对象整型键插入顺序的意外
你向对象添加键 '2'、'1'、'10' 期望按插入顺序,但它们以 '1'、'2'、'10' 的数值顺序返回。
- 46
Set 因引用相等而存入重复
你向 Set 添加了两个内容相同的对象——它们是不同引用,所以都被保留。
- 47
parseFloat 在首个非数字字符处停止
你对含尾部非数字文本的字符串调用 parseFloat——它静默停止并返回部分数字。
- 48
数组 length 赋值截断的意外
你设 arr.length = 2 来「调整大小」——索引 1 之后的元素被静默且永久删除。
- 49
负数取模得负结果
你用 % 期望正结果,但被除数为负时结果为负——破坏索引回绕。
- 50
toFixed 返回字符串而非数字
你写了 price.toFixed(2) + shipping——toFixed 返回字符串,于是 + 拼接而非相加。
未找到相关错误。