主题: javascript
-
浏览器存储:比较 cookie、Web Storage 与 IndexedDB
Cookie 会随每个请求传输,规范要求的容量较小;localStorage 和 sessionStorage 是同步字符串存储,每个源只有数 MiB;IndexedDB 是异步、支持事务的存储,共享源的较大配额。除非获准持久化,否则浏览器存储都是尽力保留,并按源划分。
-
Using fetch with timeouts and AbortController
A fetch promise rejects only on network failure, not on HTTP error status, and it has no timeout by itself. Pass an AbortSignal combined from AbortSignal.timeout and a caller's controller, check response.ok, and tell TimeoutError, AbortError, network errors and HTTP errors apart in the catch.
-
ES module builds with declared side effects shrink consumer bundles more than CommonJS builds
Hypothesis: for a library of many independent functions, a consumer that imports a few of them gets a smaller bundle when the library ships ES modules with a sideEffects declaration than when it ships CommonJS, because tree shaking depends on static import/export structure; a fixture-based comparison is proposed.
-
Web Components:自定义元素、Shadow DOM 与封装的边界
自定义元素用带连字符的标签名注册类,并提供生命周期回调;Shadow DOM 给予它作用域受限的子树,样式不会向任一方向泄漏;模板和声明式影子根提供标记。所有跨边界交互,从通过 parts 和自定义属性设置样式,到通过 ElementInternals 参与表单及关联标签,都必须有意开放。
-
When does client-side routing still pay off now that browsers offer bfcache, prerendering and cross-document view transitions?
Open question: in-page routers were adopted to avoid full page loads, at the cost of shell serving, 404 handling, scroll and focus restoration and a bundle that must arrive first; the back/forward cache, the Speculation Rules API and cross-document view transitions now address the original motivations in multi-page sites. For which sites and interaction patterns does an in-page router still measurably win?
-
适应语言区域的界面:Intl 复数规则、日期与从右向左布局
使用浏览器的 Intl 对象格式化数字、日期和相对时间,而不是手工操作字符串;按 Intl.PluralRules 类别选择复数形式,而不是判断 n == 1;使用 CSS 逻辑属性布局,让 dir=rtl 文档自行镜像,并为翻译后变长的文本留出空间。
-
Node.js 中的 ES 模块与 CommonJS
CommonJS 使用同步的 require 和 module.exports;ES 模块使用静态 import/export、顶层 await 和 import.meta。Node 根据文件扩展名以及最近的 package.json 中的 type 字段逐个判断模块类型。ES 模块要求完整文件扩展名,且没有 __dirname 和 require,但有文档说明的替代方式。
-
JavaScript 文本偏移:区分 UTF-16 码元、码点和可见字符
在每个编辑器或 API 边界处注明偏移量的单位,并在切分文本前有意识地进行转换。
-
Rolling out a service worker safely: scope, versioned caches, the waiting worker and a kill switch
A service worker that caches HTML can pin users to old code after a deploy. Register it with an explicit scope, fetch the script fresh on every update check, version caches per build and delete old ones on activate, choose per-resource strategies, decide how the waiting worker takes over, and ship a tested kill switch before the first release.
-
Keyboard navigation in composite widgets: roving tabindex, arrow keys and Escape
Tab and Shift+Tab move between components; arrow keys move inside a component such as a tab list, toolbar, menu or radio group. Implement this with a roving tabindex (one item at tabindex=0, the rest at -1) or aria-activedescendant, close transient surfaces with Escape, and never trap focus without an advertised way out.
-
Focus management in single-page interactions: dialogs, route changes and removed elements
When a page changes without a full load, keyboard focus must be moved on purpose: into a dialog when it opens and back to its trigger when it closes, to the new view's heading after a client-side route change, and to a sensible neighbour when the focused element is deleted. Otherwise focus falls to the document body and screen-reader users are sent back to the top.
-
处理 Promise 与 async/await 中的错误
async 函数会把 throw 转为 Promise 拒绝,await 则会重新抛出该错误;每个拒绝都必须有人负责处理。只包装能够恢复的操作,绝不放任 Promise 无人接管,允许部分失败时使用 allSettled,并注册兜底的 unhandledrejection 处理器。
-
The JavaScript event loop: tasks, microtasks and rendering
JavaScript runs one job at a time; the HTML event loop takes a task, then drains the whole microtask queue (promise reactions, queueMicrotask) before the next task or a rendering opportunity. Long synchronous work and endless microtask chains block input and painting.
-
通过 JSON 传输 BigInt:添加 replacer 前先定义无损字段契约
用明确的模式编码大整数,并在还原时避免悄然经过 Number 中间转换。
-
位操作基础:正确使用标志、掩码与移位
标志是二的幂,通过 OR 组合、AND 检查、AND NOT 清除;掩码和移位用于提取字段。陷阱包括运算符优先级、有符号右移、JavaScript 固定的 32 位转换,以及 Python 无界整数。应为每个标志命名,并为每次检查加括号。
机器可读: JSON