๐ค
JavaScript
Write robust JavaScript with async patterns, type coercion handling, and modern ES2023+ features.
ๅฎๅ
จ้่ฟ
ๆ่ฝ่ฏดๆ
name: JavaScript slug: javascript version: 1.0.3 description: Write robust JavaScript with async patterns, type coercion handling, and modern ES2023+ features.
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 | async.md |
| Type coercion rules | coercion.md |
| Array and object methods | collections.md |
| Modern ES features | modern.md |
Equality Traps
==coerces:"0" == falseis true โ use===alwaysNaN !== NaNโ useNumber.isNaN(), not=== NaNtypeof null === "object"โ check=== nullexplicitly- Objects compare by reference โ
{} === {}is false
this Binding
- Regular functions:
thisdepends on call site โ lost in callbacks - Arrow functions:
thisfrom lexical scope โ use for callbacks setTimeout(obj.method)losesthisโ use arrow or.bind()- Event handlers:
thisis element in regular function, undefined in arrow (if no outer this)
Closure Traps
- Loop variable captured by reference โ
letin loop or IIFE to capture value varhoisted to function scope โ creates single binding shared across iterations- Returning function from loop: all share same variable โ use
letper iteration
Array Mutation
sort(),reverse(),splice()mutate original โ usetoSorted(),toReversed(),toSpliced()(ES2023)push(),pop(),shift(),unshift()mutate โ spread[...arr, item]for immutabledelete arr[i]leaves hole โ usesplice(i, 1)to remove and reindex- Spread and
Object.assignare shallow โ nested objects still reference original
Async Pitfalls
- Forgetting
awaitreturns Promise, not value โ easy to miss without TypeScript forEachdoesn't await โ usefor...offor sequential asyncPromise.allfails fast โ one rejection rejects all, usePromise.allSettledif 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 ortoFixed()for displayparseInt("08")works now โ butparseInt("0x10")is 16, watch prefixesNumber("")is 0,Number(null)is 0 โ butNumber(undefined)is NaN- Large integers lose precision over 2^53 โ use
BigIntfor big numbers
Iteration
for...initerates keys (including inherited) โ usefor...offor valuesfor...ofon objects fails โ objects aren't iterable, useObject.entries()Object.keys()skips non-enumerable โReflect.ownKeys()gets all including symbols
Implicit Coercion
[] + []is""โ arrays coerce to strings[] + {}is"[object Object]"โ object toString{} + []is0in console โ{}parsed as block, not object"5" - 1is 4,"5" + 1is "51" โ minus coerces, plus concatenates
Strict Mode
"use strict"at top of file or function โ catches silent errors- Implicit globals throw in strict โ
x = 5without declaration fails thisis undefined in strict functions โ not global object- Duplicate parameters and
withforbidden
ๅฆไฝไฝฟ็จใJavaScriptใ๏ผ
- ๆๅผๅฐ้พ่พAI๏ผWeb ๆ iOS App๏ผ
- ็นๅปไธๆนใ็ซๅณไฝฟ็จใๆ้ฎ๏ผๆๅจๅฏน่ฏๆกไธญ่พๅ ฅไปปๅกๆ่ฟฐ
- ๅฐ้พ่พAI ไผ่ชๅจๅน้ ๅนถ่ฐ็จใJavaScriptใๆ่ฝๅฎๆไปปๅก
- ็ปๆๅณๆถๅ็ฐ๏ผๆฏๆ็ปง็ปญๅฏน่ฏไผๅ