When to Use
User needs JavaScript expertise — from core language features to modern patterns. Agent handles async/await, closures, module systems, and ES2023+ features.
Quick Reference
| Topic | File |
|---|
| Async patterns | INLINECODE0 |
| Type coercion rules |
coercion.md |
| Array and object methods |
collections.md |
| Modern ES features |
modern.md |
Equality Traps
- -
== coerces: "0" == false is true — use === always - INLINECODE7 — use
Number.isNaN(), not INLINECODE9 - INLINECODE10 — check
=== null explicitly - Objects compare by reference —
{} === {} is false
this Binding
- - Regular functions:
this depends on call site — lost in callbacks - Arrow functions:
this from lexical scope — use for callbacks - INLINECODE15 loses
this — use arrow or INLINECODE17 - Event handlers:
this is element in regular function, undefined in arrow (if no outer this)
Closure Traps
- - Loop variable captured by reference —
let in loop or IIFE to capture value - INLINECODE20 hoisted to function scope — creates single binding shared across iterations
- Returning function from loop: all share same variable — use
let per iteration
Array Mutation
- -
sort(), reverse(), splice() mutate original — use toSorted(), toReversed(), toSpliced() (ES2023) - INLINECODE28 ,
pop(), shift(), unshift() mutate — spread [...arr, item] for immutable - INLINECODE33 leaves hole — use
splice(i, 1) to remove and reindex - Spread and
Object.assign are shallow — nested objects still reference original
Async Pitfalls
- - Forgetting
await returns Promise, not value — easy to miss without TypeScript - INLINECODE37 doesn't await — use
for...of for sequential async - INLINECODE39 fails fast — one rejection rejects all, use
Promise.allSettled if need all results - Unhandled rejection crashes in Node — always
.catch() or try/catch with await
Numbers
- -
0.1 + 0.2 !== 0.3 — floating point, use integer cents or toFixed() for display - INLINECODE44 works now — but
parseInt("0x10") is 16, watch prefixes - INLINECODE46 is 0,
Number(null) is 0 — but Number(undefined) is NaN - Large integers lose precision over 2^53 — use
BigInt for big numbers
Iteration
- -
for...in iterates keys (including inherited) — use for...of for values - INLINECODE52 on objects fails — objects aren't iterable, use INLINECODE53
- INLINECODE54 skips non-enumerable —
Reflect.ownKeys() gets all including symbols
Implicit Coercion
- -
[] + [] is "" — arrays coerce to strings - INLINECODE58 is
"[object Object]" — object toString - INLINECODE60 is
0 in console — {} parsed as block, not object - INLINECODE63 is 4,
"5" + 1 is "51" — minus coerces, plus concatenates
Strict Mode
- -
"use strict" at top of file or function — catches silent errors - Implicit globals throw in strict —
x = 5 without declaration fails - INLINECODE67 is undefined in strict functions — not global object
- Duplicate parameters and
with forbidden
使用场景
用户需要JavaScript专业知识——从核心语言特性到现代编程模式。代理可处理async/await、闭包、模块系统和ES2023+特性。
快速参考
coercion.md |
| 数组和对象方法 | collections.md |
| 现代ES特性 | modern.md |
相等性陷阱
- - ==会进行类型强制转换:0 == false结果为true——始终使用===
- NaN !== NaN——应使用Number.isNaN(),而非=== NaN
- typeof null === object——需显式检查=== null
- 对象按引用比较——{} === {}结果为false
this绑定
- - 普通函数:this取决于调用位置——在回调中会丢失
- 箭头函数:this来自词法作用域——适用于回调函数
- setTimeout(obj.method)会丢失this——使用箭头函数或.bind()
- 事件处理程序:普通函数中this为元素,箭头函数中为undefined(若无外部this)
闭包陷阱
- - 循环变量通过引用捕获——使用let或IIFE来捕获值
- var提升至函数作用域——在迭代间创建单一共享绑定
- 从循环返回函数:所有函数共享同一变量——每次迭代使用let
数组变更
- - sort()、reverse()、splice()会改变原数组——使用toSorted()、toReversed()、toSpliced()(ES2023)
- push()、pop()、shift()、unshift()会改变原数组——使用展开运算符[...arr, item]实现不可变操作
- delete arr[i]会留下空洞——使用splice(i, 1)移除并重新索引
- 展开运算符和Object.assign是浅拷贝——嵌套对象仍引用原对象
异步陷阱
- - 忘记await会返回Promise而非值——没有TypeScript时容易遗漏
- forEach不会等待——使用for...of实现顺序异步
- Promise.all快速失败——一个拒绝会导致全部拒绝,如需所有结果使用Promise.allSettled
- Node中未处理的拒绝会崩溃——始终使用.catch()或try/catch配合await
数字
- - 0.1 + 0.2 !== 0.3——浮点数问题,使用整数分或toFixed()进行显示
- parseInt(08)现在能正常工作——但parseInt(0x10)结果为16,注意前缀
- Number()为0,Number(null)为0——但Number(undefined)为NaN
- 大整数超过2^53会丢失精度——使用BigInt处理大数字
迭代
- - for...in遍历键(包括继承的)——使用for...of遍历值
- for...of对对象无效——对象不可迭代,使用Object.entries()
- Object.keys()跳过不可枚举属性——Reflect.ownKeys()获取所有属性包括Symbol
隐式类型转换
- - [] + []结果为——数组被强制转换为字符串
- [] + {}结果为[object Object]——对象的toString方法
- 控制台中{} + []结果为0——{}被解析为代码块而非对象
- 5 - 1为4,5 + 1为51——减号强制转换,加号拼接
严格模式
- - 在文件或函数顶部添加use strict——捕获静默错误
- 严格模式下隐式全局变量会报错——x = 5未声明会失败
- 严格函数中this为undefined——而非全局对象
- 禁止重复参数和with语句