docs(js_notes): fix factual errors and imprecise wording

- 08-sets-maps: correct 'object order not guaranteed' -> objects keep
  insertion order except integer-like keys (reordered ascending); this
  is the real reason to prefer Map when numeric keys must stay ordered
- 05-conditionals: soften 'only 6 falsy values' (note 0n/-0 exist)
- 04-strings: annotate expected output for immutable string[0]='x'
  (silent in sloppy mode, TypeError in strict/ESM)
- 04-strings: reword exercise C hint to direction-only (no full answer)
- 09-loops: fix typo in for...in comment
pull/1034/head
Jaydon 2 weeks ago
parent 7b9ddf113c
commit e9fb78c3f3

@ -117,9 +117,10 @@ console.log(word) // hello ← 原串没变!
word = word.toUpperCase() // 赋值回去才生效
console.log(word) // HELLO
// 想直接改某个字符?改不动 —— 字符串不可变
let text = 'hello'
text[0] = 'x'
console.log(text)
text[0] = 'x' // 非严格模式:这行被静默忽略;严格模式/ESM:直接抛 TypeError
console.log(text) // hello ← 没变!要改只能整串重新赋值(见上面第 8 组)
// ═════ 轮到你 ═════
@ -146,7 +147,7 @@ console.log(email.split('@'))
// 练习 C:有 const raw = ' JavaScript '(两端有空格)
// 先去掉两端空格,再转成小写,最后打印。
// 提示:两个方法可以【链式】连着写:raw.trim().toLowerCase()
// 提示:去空格和转小写各有一个方法,两个可以【链式】连着写(前一个的结果接着点下一个)
const raw = ' JavaScript '
console.log(raw.trim().toLowerCase())

@ -61,8 +61,9 @@
//
// 4. 真假值 truthy / falsy ⭐⭐(之前埋的伏笔)
// if() 里不一定非要放 true/false —— 任何值都会被 JS 自动判成"真"或"假"。
// 假值(falsy)—— 只有这 6 个,记住就行:
// 假值(falsy)—— 常记这 6 个就够:
// false 0 ''(空字符串) null undefined NaN
// (严格说还有 0n(BigInt 的零)和 -0,都少见,先把上面 6 个记牢)
// 真值(truthy)—— 除了上面 6 个,其他全是真:
// 包括非零数字、非空字符串、[] 空数组、{} 空对象(注意!空数组空对象是真)
// if ('hello') … // 会执行
@ -103,7 +104,7 @@
// 1. if / else if / else:命中第一个成立的就停
// 2. 三元 条件 ? 真值 : 假值:简单二选一,有返回值 ⭐
// 3. switch:多分支,每个 case 别忘 break
// 4. falsy 只有 6 个:false 0 '' null undefined NaN;其余全 truthy(空数组空对象是真!)⭐⭐
// 4. falsy 常记 6 个:false 0 '' null undefined NaN(另有 0n/-0,少见);其余全 truthy(空数组空对象是真!)⭐⭐
// 5. &&/|| 返回操作数原值(非布尔):|| 取第一个真值(设默认值),&& 取第一个假值(做守卫);
// 要纯布尔用 !!。Python 同理,Java/C++ 才返回布尔 ⭐⭐

@ -43,10 +43,15 @@
// ├──────────┼──────────────────────────┼──────────────────────┤
// │ 键的类型 │ 只能字符串/symbol │ 任意类型 │
// │ 数量 │ Object.keys(o).length │ m.size(直接) │
// │ 顺序 │ 不保证 │ 保证插入顺序
// │ 顺序 │ 基本按插入序,整数键会重排│ 严格保证插入顺序
// │ 遍历 │ 需 Object.keys 等 │ 直接可迭代 │
// └──────────┴──────────────────────────┴──────────────────────┘
// 日常大多数场景用对象就够;需要"非字符串键""频繁增删""保序"时用 Map。
// ⚠️ 对象的"顺序"有个坑:字符串键按【插入顺序】,但【整数样式的键】
// (如 '2'、'10')会被自动提前、按升序排,无视你插入的次序:
// Object.keys({ b: 1, 2: 1, a: 1, 10: 1 }) // ['2','10','b','a']
// // 整数键 2,10 被提前且升序
// Map 则严格按插入序,不管键是什么类型 —— 这才是"键是数字又在意顺序时用 Map"的真正理由。
// 日常大多数场景用对象就够;需要"非字符串键""频繁增删""严格保序"时用 Map。
//
// 3. 创建时初始化
// const s = new Set(['a', 'b', 'c']) // Set:传数组

@ -24,7 +24,7 @@
//
// ── 3. for...in:遍历对象的键 ────────────────────
// for (const key in obj) → key 依次是对象的每个键名
// 注意:for...in 拿到的是【键】,要值得用 obj[key]
// 注意:for...in 拿到的是【键】,要取值就用 obj[key]
//
// ⚠️ 口诀:of 拿值(value),in 拿键/下标(index/key)
// ⚠️ 别用 for...in 遍历数组(会有意外的坑),数组用 for...of 或 for

Loading…
Cancel
Save