๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ
ๅฐ้พ™่™พๅฐ้พ™่™พAI
๐Ÿค–

JavaScript

Write robust JavaScript with async patterns, type coercion handling, and modern ES2023+ features.

ไธ‹่ฝฝ3.7k
ๆ˜Ÿๆ ‡5
็‰ˆๆœฌ1.0.3
ๅ†™ไฝœๅˆ›ไฝœ
ๅฎ‰ๅ…จ้€š่ฟ‡

ๆŠ€่ƒฝ่ฏดๆ˜Ž


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

TopicFile
Async patternsasync.md
Type coercion rulescoercion.md
Array and object methodscollections.md
Modern ES featuresmodern.md

Equality Traps

  • == coerces: "0" == false is true โ€” use === always
  • NaN !== NaN โ€” use Number.isNaN(), not === NaN
  • typeof null === "object" โ€” 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
  • setTimeout(obj.method) loses this โ€” use arrow or .bind()
  • 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
  • var 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)
  • push(), pop(), shift(), unshift() mutate โ€” spread [...arr, item] for immutable
  • delete arr[i] 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
  • forEach doesn't await โ€” use for...of for sequential async
  • Promise.all 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
  • parseInt("08") works now โ€” but parseInt("0x10") is 16, watch prefixes
  • Number("") 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
  • for...of on objects fails โ€” objects aren't iterable, use Object.entries()
  • Object.keys() skips non-enumerable โ€” Reflect.ownKeys() gets all including symbols

Implicit Coercion

  • [] + [] is "" โ€” arrays coerce to strings
  • [] + {} is "[object Object]" โ€” object toString
  • {} + [] is 0 in console โ€” {} parsed as block, not object
  • "5" - 1 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
  • this is undefined in strict functions โ€” not global object
  • Duplicate parameters and with forbidden

ๅฆ‚ไฝ•ไฝฟ็”จใ€ŒJavaScriptใ€๏ผŸ

  1. ๆ‰“ๅผ€ๅฐ้พ™่™พAI๏ผˆWeb ๆˆ– iOS App๏ผ‰
  2. ็‚นๅ‡ปไธŠๆ–นใ€Œ็ซ‹ๅณไฝฟ็”จใ€ๆŒ‰้’ฎ๏ผŒๆˆ–ๅœจๅฏน่ฏๆก†ไธญ่พ“ๅ…ฅไปปๅŠกๆ่ฟฐ
  3. ๅฐ้พ™่™พAI ไผš่‡ชๅŠจๅŒน้…ๅนถ่ฐƒ็”จใ€ŒJavaScriptใ€ๆŠ€่ƒฝๅฎŒๆˆไปปๅŠก
  4. ็ป“ๆžœๅณๆ—ถๅ‘ˆ็Žฐ๏ผŒๆ”ฏๆŒ็ปง็ปญๅฏน่ฏไผ˜ๅŒ–

็›ธๅ…ณๆŠ€่ƒฝ