<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <author>
    <name>Leo Liu</name>
  </author>
  <generator uri="https://hexo.io/">Hexo</generator>
  <id>https://liuduwei-blog.pages.dev/</id>
  <link href="https://liuduwei-blog.pages.dev/" rel="alternate"/>
  <link href="https://liuduwei-blog.pages.dev/atom.xml" rel="self"/>
  <rights>All rights reserved 2026, Leo Liu</rights>
  <subtitle>Flutter · Android · 前端技术笔记</subtitle>
  <title>Leo Liu's Blog</title>
  <updated>2026-12-01T15:40:31.640Z</updated>
  <entry>
    <author>
      <name>Leo Liu</name>
    </author>
    <category term="Flutter" scheme="https://liuduwei-blog.pages.dev/categories/Flutter/"/>
    <category term="Flutter" scheme="https://liuduwei-blog.pages.dev/tags/Flutter/"/>
    <category term="Framework" scheme="https://liuduwei-blog.pages.dev/tags/Framework/"/>
    <content>
      <![CDATA[<p><a href="#schedule">点击与调度</a> · <a href="#frame">一帧的执行顺序</a> · <a href="#render-object">局部更新</a> · <a href="#during-build">build 期间调用</a></p><p>以 Wonderous 使用的 <strong>Flutter 3.47.2</strong>（源码提交 <code>d3b14c8769</code>）为准。先看一个已经显示在屏幕上的页面：用户点击按钮，事件回调执行 <code>setState(() &#123; _selected = true; &#125;)</code>。<strong>回调返回时，<code>_selected</code> 已经变了；屏幕何时反映这个值，取决于框架何时重建该 Element，以及新配置改变了哪些渲染属性。</strong> 本文沿用《Flutter 实战》14.3 的阶段顺序，图中方法名和队列归属对应这个 SDK 版本。<a href="https://book.flutterchina.club/chapter14/flutter_app_startup.html">原文章节</a> · <a href="https://api.flutter.dev/flutter/widgets/State/setState.html">State.setState 官方说明</a></p><span id="more"></span><p><a id="schedule"></a></p><h2 id="图一-点击回调返回前，Flutter-做了什么？"><a href="#图一-点击回调返回前，Flutter-做了什么？" class="headerlink" title="图一　点击回调返回前，Flutter 做了什么？"></a>图一　点击回调返回前，Flutter 做了什么？</h2><p>先看右侧启动时的赋值，再沿左侧实线跟一次点击。虚线说明点击时调用的是哪个函数。图中假设页面空闲、允许渲染，且没有待处理的帧请求。</p><pre class="mermaid">flowchart TD    subgraph ClickFlow["点击后 · 执行路径"]        A1["setState(fn) 同步执行 fn&lt;br/&gt;调用当前 Element.markNeedsBuild()&lt;br/&gt;widgets/framework.dart:1201–1221"]        A2["已 dirty 就返回；否则标脏&lt;br/&gt;进入 BuildOwner.scheduleBuildFor&lt;br/&gt;widgets/framework.dart:5399–5403"]        A3{"未安排本轮构建通知&lt;br/&gt;且回调已注册？"}        A4["置标志为 true&lt;br/&gt;onBuildScheduled!() 调用已保存函数&lt;br/&gt;widgets/framework.dart:2999–3000"]        A5["_handleBuildScheduled()&lt;br/&gt;通过调试检查后调用 ensureVisualUpdate()&lt;br/&gt;widgets/binding.dart:1430–1459"]        A6["SchedulerBinding.ensureVisualUpdate()&lt;br/&gt;当前处于 idle，调用 scheduleFrame()&lt;br/&gt;scheduler/binding.dart:906–917"]        A7["允许渲染且没有待处理的帧请求&lt;br/&gt;platformDispatcher.scheduleFrame()&lt;br/&gt;scheduler/binding.dart:946–958"]        A8["回到 BuildOwner.scheduleBuildFor&lt;br/&gt;调用 buildScope._scheduleBuildFor(element)&lt;br/&gt;widgets/framework.dart:3002"]        A9["该 Element 若尚未入队&lt;br/&gt;就加入 BuildScope._dirtyElements&lt;br/&gt;widgets/framework.dart:2729–2734"]        A1 --&gt; A2        A2 --&gt; A3        A3 --&gt;|是| A4        A3 --&gt;|否，直接入队| A8        A4 --&gt; A5        A5 --&gt; A6        A6 --&gt; A7        A7 --&gt;|回调返回后| A8        A8 --&gt; A9    end    subgraph StartupFlow["启动时 · 注册回调"]        B1["WidgetsBinding.initInstances()&lt;br/&gt;buildOwner!.onBuildScheduled = _handleBuildScheduled&lt;br/&gt;widgets/binding.dart:477"]    end    B1 -.-&gt;|虚线指向实际调用处| A4    style B1 stroke:#f59e0b,stroke-width:2px    style A4 stroke:#f59e0b,stroke-width:2px</pre><details><summary>图一源码索引</summary><ul><li><code>setState(fn)</code>：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L1201-L1221">widgets&#x2F;framework.dart:1201–1221</a></li><li><code>WidgetsBinding.initInstances()</code>：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/binding.dart#L477">widgets&#x2F;binding.dart:477</a></li><li><code>Element.markNeedsBuild()</code>：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L5399-L5403">widgets&#x2F;framework.dart:5399–5403</a></li><li><code>BuildOwner.scheduleBuildFor</code>：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L2998">widgets&#x2F;framework.dart:2998</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L2999-L3000">widgets&#x2F;framework.dart:2999–3000</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L3002">widgets&#x2F;framework.dart:3002</a></li><li><code>_handleBuildScheduled()</code>：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/binding.dart#L1430-L1459">widgets&#x2F;binding.dart:1430–1459</a></li><li><code>SchedulerBinding.ensureVisualUpdate()</code> &#x2F; <code>scheduleFrame()</code>：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/scheduler/binding.dart#L906-L917">scheduler&#x2F;binding.dart:906–917</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/scheduler/binding.dart#L946-L958">scheduler&#x2F;binding.dart:946–958</a></li><li><code>BuildScope._scheduleBuildFor</code>：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L2729-L2734">widgets&#x2F;framework.dart:2729–2734</a></li></ul></details><h3 id="启动时，谁把函数存进了回调字段？"><a href="#启动时，谁把函数存进了回调字段？" class="headerlink" title="启动时，谁把函数存进了回调字段？"></a>启动时，谁把函数存进了回调字段？</h3><figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">_buildOwner = <span class="title class_">BuildOwner</span>();</span><br><span class="line">buildOwner!.onBuildScheduled = _handleBuildScheduled;</span><br></pre></td></tr></table></figure><p>先读图一右侧。<code>onBuildScheduled</code> 是 <code>BuildOwner</code> 上类型为 <code>VoidCallback?</code> 的字段，可以保存一个无参数、无返回值的函数。赋值右侧的 <code>_handleBuildScheduled</code> 没有括号，Dart 会取出绑定到当前 <code>WidgetsBinding</code> 实例的方法，存进这个字段。想在 IDE 里找这条连线，可以搜 <code>onBuildScheduled =</code>。单点字段名通常会跳到字段声明。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/binding.dart#L477">widgets&#x2F;binding.dart:477</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L2927">widgets&#x2F;framework.dart:2927</a></p><h3 id="点击后，哪一行真正调用它？"><a href="#点击后，哪一行真正调用它？" class="headerlink" title="点击后，哪一行真正调用它？"></a>点击后，哪一行真正调用它？</h3><figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (!_scheduledFlushDirtyElements &amp;&amp; onBuildScheduled != <span class="keyword">null</span>) &#123;</span><br><span class="line">  _scheduledFlushDirtyElements = <span class="keyword">true</span>;</span><br><span class="line">  onBuildScheduled!();</span><br><span class="line">&#125;</span><br><span class="line">buildScope.<span class="title function_">_scheduleBuildFor</span>(element);</span><br></pre></td></tr></table></figure><p>假设刚才只有 A 和 B 两个 Element 需要更新。A 先变 dirty 时，<code>_scheduledFlushDirtyElements</code> 为 <code>false</code>。框架把它设为 <code>true</code>，然后执行第 3000 行的 <code>onBuildScheduled!()</code>。这里的 <code>!</code> 表示取非空的函数值，后面的 <code>()</code> 才执行函数。<strong>在第 3000 行打断点并单步进入，就会到 <code>WidgetsBinding._handleBuildScheduled()</code>。</strong> <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L2998-L3002">widgets&#x2F;framework.dart:2998–3002</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/binding.dart#L1430">widgets&#x2F;binding.dart:1430</a></p><p>回调返回后，A 继续执行第 3002 行并入队。若 B 在列表处理前也变 dirty，此时标志已经为 <code>true</code>，B 会跳过 <code>if</code>，直接走第 3002 行。两个 Element 都能重建，只需通知一次。若是同一个 A 连续调用两次 <code>setState</code>，两次状态回调都会执行，第二次 <code>markNeedsBuild</code> 会在发现 A 已经 dirty 时提前返回。处理完构建列表后，<code>buildScope</code> 在 <code>finally</code> 中将通知标志复位。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L2729-L2734">widgets&#x2F;framework.dart:2729–2734</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L5399-L5403">widgets&#x2F;framework.dart:5399–5403</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L3124-L3127">widgets&#x2F;framework.dart:3124–3127</a></p><h3 id="为什么从-widgets-binding-dart-跳到了-scheduler-binding-dart？"><a href="#为什么从-widgets-binding-dart-跳到了-scheduler-binding-dart？" class="headerlink" title="为什么从 widgets&#x2F;binding.dart 跳到了 scheduler&#x2F;binding.dart？"></a>为什么从 widgets&#x2F;binding.dart 跳到了 scheduler&#x2F;binding.dart？</h3><p><code>_handleBuildScheduled()</code> 在 <strong>widgets&#x2F;binding.dart 第 1430 行</strong>开始。沿着正常点击的路径往下读，调试检查过后，<strong>第 1459 行</strong>就是 <code>ensureVisualUpdate();</code>。默认的 <code>WidgetsFlutterBinding</code> 同时混入了 <code>WidgetsBinding</code> 和 <code>SchedulerBinding</code>，这两个方法都可以在同一个 binding 实例上调用。在第 1459 行对方法名点“跳转到定义”，会到 <strong>scheduler&#x2F;binding.dart 第 906 行</strong>。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/binding.dart#L1430-L1459">widgets&#x2F;binding.dart:1430–1459</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/binding.dart#L2128-L2136">widgets&#x2F;binding.dart:2128–2136</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/scheduler/binding.dart#L906">scheduler&#x2F;binding.dart:906</a></p><h3 id="到了-ensureVisualUpdate，一定会请求一帧吗？"><a href="#到了-ensureVisualUpdate，一定会请求一帧吗？" class="headerlink" title="到了 ensureVisualUpdate，一定会请求一帧吗？"></a>到了 ensureVisualUpdate，一定会请求一帧吗？</h3><figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">void</span> <span class="title function_">ensureVisualUpdate</span>() &#123;</span><br><span class="line">  <span class="keyword">switch</span> (schedulerPhase) &#123;</span><br><span class="line">    <span class="keyword">case</span> <span class="title class_">SchedulerPhase</span>.idle:</span><br><span class="line">    <span class="keyword">case</span> <span class="title class_">SchedulerPhase</span>.postFrameCallbacks:</span><br><span class="line">      <span class="title function_">scheduleFrame</span>();</span><br><span class="line">      <span class="keyword">return</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="title class_">SchedulerPhase</span>.transientCallbacks:</span><br><span class="line">    <span class="keyword">case</span> <span class="title class_">SchedulerPhase</span>.midFrameMicrotasks:</span><br><span class="line">    <span class="keyword">case</span> <span class="title class_">SchedulerPhase</span>.persistentCallbacks:</span><br><span class="line">      <span class="keyword">return</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>页面静止时点击，通常进入 <code>idle</code> 分支；在帧末回调中则进入 <code>postFrameCallbacks</code> 分支。两者都会调用 <code>scheduleFrame()</code>。如果是动画监听器在本帧的临时回调阶段调用 <code>setState</code>，此时处于 <code>transientCallbacks</code>，本帧的 build 还没开始。<code>ensureVisualUpdate()</code> 可以直接返回，Element 仍会入队，赶上本帧。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/scheduler/binding.dart#L906-L917">scheduler&#x2F;binding.dart:906–917</a></p><p><code>scheduleFrame()</code> 还有一道检查：已有待处理的帧请求，或当前禁用了帧，就提前返回。通过检查才会执行第 957 行的 <code>platformDispatcher.scheduleFrame()</code>。这一行只向引擎请求帧；随后调用栈退回 <code>BuildOwner.scheduleBuildFor</code>，继续第 3002 行。真正的 build 要等图二的帧回调。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/scheduler/binding.dart#L946-L958">scheduler&#x2F;binding.dart:946–958</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L3002">widgets&#x2F;framework.dart:3002</a></p><p><a id="frame"></a></p><h2 id="图二-帧到来后，哪些工作按顺序执行？"><a href="#图二-帧到来后，哪些工作按顺序执行？" class="headerlink" title="图二　帧到来后，哪些工作按顺序执行？"></a>图二　帧到来后，哪些工作按顺序执行？</h2><p>沿左列跟一次已经显示的原生 Flutter 页面上的点击。右列是提前保存的函数，虚线指向它们被取出执行的位置。</p><pre class="mermaid">flowchart TD    subgraph FrameOrder["普通帧的执行顺序"]        F1["SchedulerBinding.scheduleFrame()&lt;br/&gt;先确认入口已注册，再向引擎请求帧&lt;br/&gt;scheduler/binding.dart:956–957"]        F2["PlatformDispatcher.scheduleFrame()&lt;br/&gt;调用 native；引擎安排等待 VSync&lt;br/&gt;platform_dispatcher.dart:881–884&lt;br/&gt;animator.cc:263–282"]        F3["帧到来 · 引擎调用 Dart 的 _beginFrame&lt;br/&gt;dispatcher 执行已注册的 onBeginFrame&lt;br/&gt;platform_configuration.cc:483–487&lt;br/&gt;platform_dispatcher.dart:431–432"]        F4["_handleBeginFrame → handleBeginFrame&lt;br/&gt;调度器执行动画等临时回调&lt;br/&gt;scheduler/binding.dart:1177, 1254–1273"]        F5["引擎清空微任务，再调用 Dart 的 _drawFrame&lt;br/&gt;dispatcher 执行已注册的 onDrawFrame&lt;br/&gt;platform_configuration.cc:489–491&lt;br/&gt;platform_dispatcher.dart:449–450"]        F6["_handleDrawFrame → handleDrawFrame&lt;br/&gt;遍历持久回调，执行 callback(timeStamp)&lt;br/&gt;scheduler/binding.dart:1198, 1343–1346, 1430"]        F7["轮到 _handlePersistentFrameCallback&lt;br/&gt;执行其中的 drawFrame()&lt;br/&gt;rendering/binding.dart:557–560"]        F8["动态分派到 WidgetsBinding.drawFrame()&lt;br/&gt;先 buildScope(rootElement)，再 super.drawFrame()&lt;br/&gt;widgets/binding.dart:1569–1574"]        F9["RendererBinding.drawFrame()&lt;br/&gt;依次处理 layout、compositing bits、paint&lt;br/&gt;rendering/binding.dart:691–694"]        F10["若 sendFramesToEngine 为 true&lt;br/&gt;逐个视图提交场景，再 flushSemantics()&lt;br/&gt;rendering/binding.dart:695–701"]        F11["回到 WidgetsBinding，执行 finalizeTree()&lt;br/&gt;持久回调全部结束后，调度器执行帧末回调&lt;br/&gt;widgets/binding.dart:1578&lt;br/&gt;scheduler/binding.dart:1348–1358"]        F1 --&gt; F2 --&gt; F3 --&gt; F4 --&gt; F5 --&gt; F6 --&gt; F7 --&gt; F8 --&gt; F9 --&gt; F10 --&gt; F11    end    subgraph CallbackReg["回调注册在哪里？"]        R1["请求前 · 仅在字段为空时赋值&lt;br/&gt;onBeginFrame ??= _handleBeginFrame&lt;br/&gt;onDrawFrame ??= _handleDrawFrame&lt;br/&gt;scheduler/binding.dart:888–891"]        R2["启动时 · RendererBinding.initInstances&lt;br/&gt;把 _handlePersistentFrameCallback&lt;br/&gt;加入调度器的 _persistentCallbacks&lt;br/&gt;rendering/binding.dart:61&lt;br/&gt;scheduler/binding.dart:781–783"]    end    R1 -.-&gt;|onBeginFrame| F3    R1 -.-&gt;|onDrawFrame| F5    R2 -.-&gt;|_persistentCallbacks| F6    style R1 stroke:#f59e0b,stroke-width:2px    style R2 stroke:#f59e0b,stroke-width:2px</pre><details><summary>图二源码索引</summary><ul><li><code>SchedulerBinding.scheduleFrame()</code>：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/scheduler/binding.dart#L956-L957">scheduler&#x2F;binding.dart:956–957</a> · <code>ensureFrameCallbacksRegistered()</code>：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/scheduler/binding.dart#L888-L891">scheduler&#x2F;binding.dart:888–891</a></li><li><code>PlatformDispatcher.scheduleFrame()</code> &amp; <code>Animator</code>：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/engine/src/flutter/lib/ui/platform_dispatcher.dart#L881-L884">platform_dispatcher.dart:881–884</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/engine/src/flutter/shell/common/animator.cc#L263-L282">animator.cc:263–282</a></li><li><code>_beginFrame</code> &#x2F; <code>onBeginFrame</code>：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/engine/src/flutter/lib/ui/window/platform_configuration.cc#L483-L487">platform_configuration.cc:483–487</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/engine/src/flutter/lib/ui/platform_dispatcher.dart#L431-L432">platform_dispatcher.dart:431–432</a></li><li><code>_handleBeginFrame</code> → <code>handleBeginFrame</code>：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/scheduler/binding.dart#L1177">scheduler&#x2F;binding.dart:1177</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/scheduler/binding.dart#L1254-L1273">scheduler&#x2F;binding.dart:1254–1273</a></li><li><code>_drawFrame</code> &#x2F; <code>onDrawFrame</code>：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/engine/src/flutter/lib/ui/window/platform_configuration.cc#L489-L491">platform_configuration.cc:489–491</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/engine/src/flutter/lib/ui/platform_dispatcher.dart#L449-L450">platform_dispatcher.dart:449–450</a></li><li><code>_handleDrawFrame</code> → <code>handleDrawFrame</code>：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/scheduler/binding.dart#L1198">scheduler&#x2F;binding.dart:1198</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/scheduler/binding.dart#L1343-L1346">scheduler&#x2F;binding.dart:1343–1346</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/scheduler/binding.dart#L1430">scheduler&#x2F;binding.dart:1430</a></li><li><code>RendererBinding.initInstances</code> &#x2F; <code>_persistentCallbacks</code>：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/binding.dart#L61">rendering&#x2F;binding.dart:61</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/scheduler/binding.dart#L781-L783">scheduler&#x2F;binding.dart:781–783</a></li><li><code>_handlePersistentFrameCallback</code> &#x2F; <code>drawFrame</code>：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/binding.dart#L557-L560">rendering&#x2F;binding.dart:557–560</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/binding.dart#L1569-L1574">widgets&#x2F;binding.dart:1569–1574</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/binding.dart#L691-L694">rendering&#x2F;binding.dart:691–694</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/binding.dart#L695-L701">rendering&#x2F;binding.dart:695–701</a></li><li><code>finalizeTree()</code> &amp; 帧末回调：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/binding.dart#L1578">widgets&#x2F;binding.dart:1578</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/scheduler/binding.dart#L1348-L1358">scheduler&#x2F;binding.dart:1348–1358</a></li></ul></details><h3 id="请求发出前，谁把两个入口交给了引擎？"><a href="#请求发出前，谁把两个入口交给了引擎？" class="headerlink" title="请求发出前，谁把两个入口交给了引擎？"></a>请求发出前，谁把两个入口交给了引擎？</h3><p>从图一最后的帧请求往上看一行。<code>SchedulerBinding.scheduleFrame()</code> 先执行 <code>ensureFrameCallbacksRegistered()</code>，下一行才调用 <code>platformDispatcher.scheduleFrame()</code>。打开前一个方法，会看到两个函数赋值。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/scheduler/binding.dart#L956-L958">scheduler&#x2F;binding.dart:956–958</a></p><figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">void</span> <span class="title function_">ensureFrameCallbacksRegistered</span>() &#123;</span><br><span class="line">  platformDispatcher.onBeginFrame ??= _handleBeginFrame;</span><br><span class="line">  platformDispatcher.onDrawFrame ??= _handleDrawFrame;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p><code>??=</code> 表示字段为空时才赋值。右边的两个方法名都没有括号。这里保存了两个入口：开始一帧时调用 <code>_handleBeginFrame</code>，进入绘制阶段时调用 <code>_handleDrawFrame</code>。若此前已经注册，本次点击可以直接沿用。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/scheduler/binding.dart#L888-L891">scheduler&#x2F;binding.dart:888–891</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/engine/src/flutter/lib/ui/platform_dispatcher.dart#L422-L432">platform_dispatcher.dart:422–432</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/engine/src/flutter/lib/ui/platform_dispatcher.dart#L440-L450">platform_dispatcher.dart:440–450</a></p><h3 id="为什么在-scheduleFrame-上单步，走不到-drawFrame-？"><a href="#为什么在-scheduleFrame-上单步，走不到-drawFrame-？" class="headerlink" title="为什么在 scheduleFrame() 上单步，走不到 drawFrame()？"></a>为什么在 scheduleFrame() 上单步，走不到 drawFrame()？</h3><p>这里跨了两次调用栈。第一次是应用向引擎<strong>请求</strong>帧：<code>PlatformDispatcher.scheduleFrame()</code> 通过 native 接口发出请求，原生引擎的 <code>Animator</code> 安排等待显示器的垂直同步信号（VSync）。这次 Dart 调用可以返回，图一中的 Element 接着入队。第二次是帧到来时<strong>引擎回调 Dart</strong>。要跟后半段，可以在 <code>_handleDrawFrame</code> 另下一个断点。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/engine/src/flutter/lib/ui/platform_dispatcher.dart#L881-L884">platform_dispatcher.dart:881–884</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/engine/src/flutter/lib/ui/window/platform_configuration.cc#L702-L705">platform_configuration.cc:702–705</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/engine/src/flutter/shell/common/engine.cc#L510-L511">engine.cc:510–511</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/engine/src/flutter/shell/common/animator.cc#L263-L282">animator.cc:263–282</a></p><p>原生引擎的 <code>PlatformConfiguration::BeginFrame</code> 把顺序写得很清楚：第 484 行调用 Dart 的 <code>_beginFrame</code>，第 489 行处理这期间排入的微任务，第 491 行才调用 Dart 的 <code>_drawFrame</code>。两个同名的 Dart 入口都在 <code>hooks.dart</code>，各自转给 <code>PlatformDispatcher.instance</code>。其中 <code>onBeginFrame</code> 让调度器执行动画等临时回调，<code>onDrawFrame</code> 才接上图里的持久回调。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/engine/src/flutter/lib/ui/window/platform_configuration.cc#L483-L491">platform_configuration.cc:483–491</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/engine/src/flutter/lib/ui/hooks.dart#L401-L404">hooks.dart:401–404</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/engine/src/flutter/lib/ui/hooks.dart#L412-L414">hooks.dart:412–414</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/engine/src/flutter/lib/ui/platform_dispatcher.dart#L431-L432">platform_dispatcher.dart:431–432</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/engine/src/flutter/lib/ui/platform_dispatcher.dart#L449-L450">platform_dispatcher.dart:449–450</a></p><h3 id="handleDrawFrame-如何找到渲染框架的回调？"><a href="#handleDrawFrame-如何找到渲染框架的回调？" class="headerlink" title="handleDrawFrame() 如何找到渲染框架的回调？"></a>handleDrawFrame() 如何找到渲染框架的回调？</h3><p><code>onDrawFrame</code> 中保存的是调度器的 <code>_handleDrawFrame</code>。在这条普通帧路径上，它在第 1198 行调用 <code>handleDrawFrame()</code>。读到这里会碰到一个循环，里面只有变量 <code>callback</code>。要知道它指向谁，需要同时看注册点和调用点。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/scheduler/binding.dart#L1180-L1199">scheduler&#x2F;binding.dart:1180–1199</a></p><ul><li><strong>注册点。</strong> 启动时，<code>RendererBinding.initInstances()</code> 执行 <code>addPersistentFrameCallback(_handlePersistentFrameCallback)</code>。调度器把传进来的函数加入 <code>_persistentCallbacks</code>，以后每帧都能在这个列表里找到它。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/binding.dart#L53-L62">rendering&#x2F;binding.dart:53–62</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/scheduler/binding.dart#L781-L783">scheduler&#x2F;binding.dart:781–783</a></li><li><strong>调用点。</strong> <code>handleDrawFrame()</code> 遍历这个列表的快照；轮到渲染回调时，<code>_invokeFrameCallback</code> 内部的 <code>callback(timeStamp)</code> 实际执行的就是 <code>RendererBinding._handlePersistentFrameCallback(timeStamp)</code>。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/scheduler/binding.dart#L1343-L1346">scheduler&#x2F;binding.dart:1343–1346</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/scheduler/binding.dart#L1429-L1431">scheduler&#x2F;binding.dart:1429–1431</a></li></ul><h3 id="为什么在-RendererBinding-里写-drawFrame-，会先进入-WidgetsBinding？"><a href="#为什么在-RendererBinding-里写-drawFrame-，会先进入-WidgetsBinding？" class="headerlink" title="为什么在 RendererBinding 里写 drawFrame()，会先进入 WidgetsBinding？"></a>为什么在 RendererBinding 里写 drawFrame()，会先进入 WidgetsBinding？</h3><figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">void</span> <span class="title function_">_handlePersistentFrameCallback</span>(<span class="title class_">Duration</span> timeStamp) &#123;</span><br><span class="line">  <span class="title function_">drawFrame</span>();</span><br><span class="line">  <span class="title function_">_scheduleMouseTrackerUpdate</span>();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>看第 558 行：<code>drawFrame()</code> 相当于 <code>this.drawFrame()</code>。这里的 <code>this</code> 是应用实际使用的 <code>WidgetsFlutterBinding</code> 实例。它在 <code>RendererBinding</code> 之后混入了覆盖该方法的 <code>WidgetsBinding</code>，所以会先进入 <code>WidgetsBinding.drawFrame()</code>。做完 build 后，<code>super.drawFrame()</code> 才进入 <code>RendererBinding.drawFrame()</code> 处理布局与绘制。返回时，widgets 层继续执行 <code>finalizeTree()</code>。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/binding.dart#L557-L560">rendering&#x2F;binding.dart:557–560</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/binding.dart#L2128-L2136">widgets&#x2F;binding.dart:2128–2136</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/binding.dart#L1569-L1578">widgets&#x2F;binding.dart:1569–1578</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/binding.dart#L691-L702">rendering&#x2F;binding.dart:691–702</a></p><p><strong>跟一次真实点击：</strong> 等页面静止，在 <code>onDrawFrame</code> 的执行处、持久回调里的 <code>drawFrame()</code>、以及 <code>buildOwner!.buildScope(rootElement!)</code> 各设一个断点。点击后依次继续执行，就能从同一帧看到引擎通知、回调分派和 Widget 重建。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/engine/src/flutter/lib/ui/platform_dispatcher.dart#L450">platform_dispatcher.dart:450</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/binding.dart#L558">rendering&#x2F;binding.dart:558</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/binding.dart#L1571">widgets&#x2F;binding.dart:1571</a></p><p>读这张图时要区分<strong>框架进入某个阶段</strong>和<strong>某个节点在这个阶段有工作</strong>。例如，只改一个方块的颜色，本帧照样会调用 <code>flushLayout()</code>，但这次颜色变化没有给布局队列增加节点。文章把两层 <code>drawFrame</code> 合成了一段伪代码；源码中 <code>WidgetsBinding</code> 负责 build 和最后的 <code>finalizeTree</code>，<code>RendererBinding</code> 负责中间的布局、绘制、提交场景和语义更新。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/binding.dart#L1569-L1579">WidgetsBinding.drawFrame</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/binding.dart#L691-L702">RendererBinding.drawFrame</a> · <a href="https://api.flutter.dev/flutter/widgets/WidgetsBinding/drawFrame.html">官方帧生命周期</a></p><p><a id="render-object"></a></p><h2 id="图三-一次-build-怎样传到-RenderObject？"><a href="#图三-一次-build-怎样传到-RenderObject？" class="headerlink" title="图三　一次 build 怎样传到 RenderObject？"></a>图三　一次 build 怎样传到 RenderObject？</h2><p>假设同一位置继续使用 <code>SizedBox(width: width, height: 40, child: ColoredBox(color: color))</code>，类型和 key 都保持一致。分别只改 <code>width</code>、只改 <code>color</code>，或只改一个没有用于构建的字段。下图跟踪这次更新给渲染管线增加的工作。</p><pre class="mermaid">flowchart TD    E1["BuildOwner.buildScope → _flushDirtyElements&lt;br/&gt;按深度排序，逐个调用 _tryRebuild(element)&lt;br/&gt;widgets/framework.dart:3124, 2811–2821"]    E2["_tryRebuild 调用 element.rebuild()&lt;br/&gt;Element.rebuild 再调用 performRebuild()&lt;br/&gt;widgets/framework.dart:2763, 5542"]    E3["StatefulElement 委托给 ComponentElement&lt;br/&gt;built = build() → state.build(this)&lt;br/&gt;widgets/framework.dart:5990–5996, 5830, 5944"]    E4["updateChild 的六条路径（见下文展开）&lt;br/&gt;本例走第 5 条：child.update(newWidget)&lt;br/&gt;widgets/framework.dart:5854, 4035–4050"]    E5["RenderObjectElement.update → _performRebuild&lt;br/&gt;调用新 Widget 的 updateRenderObject&lt;br/&gt;widgets/framework.dart:6819–6827, 6850"]    L1["只改宽度&lt;br/&gt;SizedBox 写入附加约束&lt;br/&gt;setter 调用 markNeedsLayout&lt;br/&gt;widgets/basic.dart:2808–2815&lt;br/&gt;rendering/proxy_box.dart:226–233"]    L2["布局边界入队&lt;br/&gt;flushLayout 处理仍脏的边界&lt;br/&gt;rendering/object.dart:2682–2694, 1172–1175"]    L3["_layoutWithoutResize&lt;br/&gt;performLayout 后标记绘制&lt;br/&gt;rendering/object.dart:2771, 2783"]    M1["相关属性都相同&lt;br/&gt;约束、颜色等 setter 提前返回&lt;br/&gt;本次没有新增布局或绘制&lt;br/&gt;rendering/proxy_box.dart:228–230&lt;br/&gt;widgets/basic.dart:8439–8457"]    C1["只改颜色&lt;br/&gt;ColoredBox 写入 color&lt;br/&gt;setter 调用 markNeedsPaint&lt;br/&gt;widgets/basic.dart:8415–8420, 8439–8445"]    P1["markNeedsPaint 向已有重绘边界传播并入队&lt;br/&gt;flushPaint 重绘仍脏且图层已挂接的边界&lt;br/&gt;rendering/object.dart:3344–3362, 1319–1326"]    E1 --&gt; E2 --&gt; E3 --&gt; E4 --&gt; E5    E5 --&gt; L1 --&gt; L2 --&gt; L3 --&gt; P1    E5 --&gt; M1    E5 --&gt; C1 --&gt; P1    style L1 stroke:#3b82f6,stroke-width:2px    style M1 stroke:#9ca3af,stroke-width:2px    style C1 stroke:#10b981,stroke-width:2px</pre><details><summary>图三源码索引</summary><ul><li><code>buildScope</code> &#x2F; <code>_flushDirtyElements</code>：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L3124">widgets&#x2F;framework.dart:3124</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L2811-L2821">widgets&#x2F;framework.dart:2811–2821</a></li><li><code>_tryRebuild</code> &#x2F; <code>Element.rebuild</code>：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L2763">widgets&#x2F;framework.dart:2763</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L5542">widgets&#x2F;framework.dart:5542</a></li><li><code>StatefulElement</code> &#x2F; <code>ComponentElement</code>：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L5990-L5996">widgets&#x2F;framework.dart:5990–5996</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L5830">widgets&#x2F;framework.dart:5830</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L5944">widgets&#x2F;framework.dart:5944</a></li><li><code>updateChild</code>：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L5854">widgets&#x2F;framework.dart:5854</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L4035-L4050">widgets&#x2F;framework.dart:4035–4050</a></li><li><code>RenderObjectElement.update</code>：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L6819-L6827">widgets&#x2F;framework.dart:6819–6827</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L6850">widgets&#x2F;framework.dart:6850</a></li><li>只改宽度（<code>SizedBox</code> &#x2F; <code>RenderConstrainedBox</code>）：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/basic.dart#L2808-L2815">widgets&#x2F;basic.dart:2808–2815</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/proxy_box.dart#L226-L233">rendering&#x2F;proxy_box.dart:226–233</a></li><li>相关属性都相同（提前返回）：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/proxy_box.dart#L228-L230">rendering&#x2F;proxy_box.dart:228–230</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/basic.dart#L8439-L8457">widgets&#x2F;basic.dart:8439–8457</a></li><li>只改颜色（<code>ColoredBox</code>）：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/basic.dart#L8415-L8420">widgets&#x2F;basic.dart:8415–8420</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/basic.dart#L8439-L8445">widgets&#x2F;basic.dart:8439–8445</a></li><li>布局与绘制边界处理：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/object.dart#L2682-L2694">rendering&#x2F;object.dart:2682–2694</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/object.dart#L1172-L1175">rendering&#x2F;object.dart:1172–1175</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/object.dart#L2771">rendering&#x2F;object.dart:2771</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/object.dart#L2783">rendering&#x2F;object.dart:2783</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/object.dart#L3344-L3362">rendering&#x2F;object.dart:3344–3362</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/object.dart#L1319-L1326">rendering&#x2F;object.dart:1319–1326</a></li></ul></details><h3 id="沿源码跟一次-Element-rebuild"><a href="#沿源码跟一次-Element-rebuild" class="headerlink" title="沿源码跟一次 Element.rebuild()"></a>沿源码跟一次 Element.rebuild()</h3><p>在图二的 <code>buildOwner!.buildScope(rootElement!)</code> 单步进入，最后会看到 <code>buildScope._flushDirtyElements(...)</code>。第 2813 行排序，第 2820 行调用 <code>_tryRebuild(element)</code>，<strong>真正写着 <code>element.rebuild();</code> 的是第 2763 行</strong>。比较器用 <code>a.depth - b.depth</code> 排序，所以祖先先于后代。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/binding.dart#L1571">widgets&#x2F;binding.dart:1571</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L3124">widgets&#x2F;framework.dart:3124</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L2811-L2821">widgets&#x2F;framework.dart:2811–2821</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L3629-L3634">widgets&#x2F;framework.dart:3629–3634</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L2763">widgets&#x2F;framework.dart:2763</a></p><p>进入 <code>Element.rebuild</code>，通过 active、dirty 等检查后，第 5542 行调用 <code>performRebuild()</code>。这个例子中接收者是持有页面 State 的 <code>StatefulElement</code>。它先处理可能的依赖变化，再调 <code>super.performRebuild()</code>。父类 <code>ComponentElement</code> 第 5830 行的 <code>built = build();</code> 会进入 <code>StatefulElement.build()</code>；这个方法只有一句 <code>state.build(this)</code>。所以传给你页面 <code>build(context)</code> 的 <code>context</code> 就是这个 Element。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L5516-L5521">widgets&#x2F;framework.dart:5516–5521</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L5542">widgets&#x2F;framework.dart:5542</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L5990-L5996">widgets&#x2F;framework.dart:5990–5996</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L5830">widgets&#x2F;framework.dart:5830</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L5944">widgets&#x2F;framework.dart:5944</a></p><p><a id="update-child"></a></p><h3 id="展开图三：updateChild-的每一条分支"><a href="#展开图三：updateChild-的每一条分支" class="headerlink" title="展开图三：updateChild 的每一条分支"></a>展开图三：updateChild 的每一条分支</h3><p>假设上次 <code>build</code> 返回 <code>SizedBox(width: 80)</code>，这次返回 <code>SizedBox(width: 120)</code>。走到下面这一行时，新的 Widget 已经创建，旧的 Element 还在树上。调用这行的是<strong>父 Element</strong>。它要决定：这次构建结束后，我该把哪个 Element 存回自己的 <code>_child</code>？ <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L5853-L5855">widgets&#x2F;framework.dart:5853–5855</a></p><figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">_child = <span class="title function_">updateChild</span>(_child, built, slot);</span><br></pre></td></tr></table></figure><ul><li><code>child</code> 是右侧传入的旧 <strong>Element</strong>。它的 <code>child.widget</code> 仍能读到上一次的 Widget 配置。</li><li><code>newWidget</code> 是刚返回的 <strong>Widget</strong>，这里就是 <code>built</code>。<code>newSlot</code> 是父节点给这个位置的标记，允许为 null。多子节点重排时，即使保留同一个 Element，也可能需要换 slot。</li><li>返回值是 <code>Element?</code>。父节点把结果写回左侧的 <code>_child</code>。这里 <code>State.build</code> 返回非空 Widget；下表的新 Widget 为空两条路径，可以在单子节点组件更新它的可选 child 时看到。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L3995-L4004">widgets&#x2F;framework.dart:3995–4004</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L7131-L7136">widgets&#x2F;framework.dart:7131–7136</a></li></ul><h4 id="先看有没有，再看能否复用"><a href="#先看有没有，再看能否复用" class="headerlink" title="先看有没有，再看能否复用"></a>先看有没有，再看能否复用</h4><p>按源码顺序读：第 3996 行先问“新 Widget 为空吗？”；新 Widget 存在时，第 4004 行再问“有旧 Element 吗？”。只有两个对象都存在，才需要比较 <code>child.widget</code> 和 <code>newWidget</code>。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L3995-L4005">widgets&#x2F;framework.dart:3995–4005</a></p><blockquote><p><strong>updateChild 的六条路径</strong> · 粗箭头与绿框节点跟踪同一个位置的 <code>SizedBox</code> 从宽 80 改为 120。数字对应下表，行号对应 <code>widgets/framework.dart</code>。</p></blockquote><pre class="mermaid">flowchart TD    D1{"新 Widget 为空？&lt;br/&gt;newWidget == null"}    D2{"有旧 Element？&lt;br/&gt;child != null"}    P1["1 · 位置保持空&lt;br/&gt;直接返回 null&lt;br/&gt;第 3996–4000 行"]    P2["2 · 移除旧节点&lt;br/&gt;deactivateChild(child)，返回 null&lt;br/&gt;第 3997–4000 行"]    D3{"有旧 Element？&lt;br/&gt;child != null"}    P3["3 · 填入新节点&lt;br/&gt;inflateWidget(newWidget, newSlot)&lt;br/&gt;第 4068–4073 行"]    D4{"配置对象相等？&lt;br/&gt;child.widget == newWidget"}    P4["4 · 直接复用&lt;br/&gt;按需更新 slot，跳过 child.update&lt;br/&gt;第 4027–4035 行"]    D5{"能更新复用？&lt;br/&gt;Widget.canUpdate 为 true"}    P5["5 · 更新配置（宽度 80 → 120）&lt;br/&gt;按需更新 slot，调用 child.update&lt;br/&gt;第 4035–4059 行"]    P6["6 · 替换此位置的节点&lt;br/&gt;先 deactivateChild，再 inflateWidget&lt;br/&gt;第 4060–4067 行"]    D1 --&gt;|是| D2    D1 ==&gt;|否 · 有新 Widget| D3    D2 --&gt;|否| P1    D2 --&gt;|是| P2    D3 --&gt;|否| P3    D3 ==&gt;|是| D4    D4 --&gt;|是| P4    D4 ==&gt;|否| D5    D5 ==&gt;|是| P5    D5 --&gt;|否| P6    style P5 stroke:#10b981,stroke-width:3px</pre><details><summary>updateChild 判定点源码索引</summary><ul><li><code>newWidget == null</code>：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L3996">widgets&#x2F;framework.dart:3996</a></li><li><code>child != null</code>（新 Widget 为空时）：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L3997">widgets&#x2F;framework.dart:3997</a></li><li><code>child != null</code>（新 Widget 非空时）：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L4004">widgets&#x2F;framework.dart:4004</a></li><li><code>hasSameSuperclass &amp;&amp; child.widget == newWidget</code>：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L4021-L4034">widgets&#x2F;framework.dart:4021–4034</a></li><li><code>hasSameSuperclass &amp;&amp; Widget.canUpdate</code>（<code>runtimeType</code> 与 <code>key</code> 分别相等）：<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L4035">widgets&#x2F;framework.dart:4035</a></li></ul></details><table><thead><tr><th align="left">路径</th><th align="left">条件</th><th align="left">做什么，返回谁</th><th align="left">拿例子对照源码</th></tr></thead><tbody><tr><td align="left"><strong>1</strong></td><td align="left">新 Widget 为空，旧 Element 也为空。</td><td align="left">直接返回 <code>null</code>。</td><td align="left">可选的 child 上次没填，这次也没填。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L3995-L4001">widgets&#x2F;framework.dart:3995–4001</a></td></tr><tr><td align="left"><strong>2</strong></td><td align="left">新 Widget 为空，有旧 Element。</td><td align="left">调用 <code>deactivateChild(child)</code>，然后返回 <code>null</code>。</td><td align="left">外层 SizedBox 保留，内层从 <code>child: ColoredBox(…)</code> 变为 <code>child: null</code>。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L3996-L4000">widgets&#x2F;framework.dart:3996–4000</a></td></tr><tr><td align="left"><strong>3</strong></td><td align="left">有新 Widget，旧 Element 为空。</td><td align="left">调用 <code>inflateWidget(newWidget, newSlot)</code>，返回它取得的 Element。</td><td align="left">上次没有 child，这次放入一个 ColoredBox。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L4068-L4073">widgets&#x2F;framework.dart:4068–4073</a></td></tr><tr><td align="left"><strong>4</strong></td><td align="left">两者都有，且 <code>child.widget == newWidget</code>。</td><td align="left">slot 变化就先更新 slot，然后返回原 <code>child</code>。跳过 <code>child.update</code>。</td><td align="left">父组件连续两次返回同一个 <code>const SizedBox(width: 80)</code> 实例。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L4027-L4035">widgets&#x2F;framework.dart:4027–4035</a></td></tr><tr><td align="left"><strong>5</strong></td><td align="left">两者都有，Widget 不相等，但 <code>Widget.canUpdate</code> 成立。</td><td align="left">slot 变化就先更新 slot；随后 <code>child.update(newWidget)</code>，返回原 <code>child</code>。</td><td align="left">同一位置的 SizedBox 从宽 80 改为 120，两次 key 都是 null。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L4035-L4038">widgets&#x2F;framework.dart:4035–4038</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L4050-L4059">widgets&#x2F;framework.dart:4050–4059</a></td></tr><tr><td align="left"><strong>6</strong></td><td align="left">两者都有，但前两项复用判断都没通过。</td><td align="left">先停用传进来的 <code>child</code>，再 <code>inflateWidget</code>，返回后者取得的 Element。</td><td align="left">SizedBox 换成 Padding，即使 key 相同，也走这里。同类型从 <code>ValueKey(1)</code> 换成 <code>ValueKey(2)</code> 也一样。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L4060-L4067">widgets&#x2F;framework.dart:4060–4067</a></td></tr></tbody></table><h4 id="第-4-和第-5-条都返回同一个-Element，区别在哪里？"><a href="#第-4-和第-5-条都返回同一个-Element，区别在哪里？" class="headerlink" title="第 4 和第 5 条都返回同一个 Element，区别在哪里？"></a>第 4 和第 5 条都返回同一个 Element，区别在哪里？</h4><p><strong>第 4 条连配置更新都可以跳过。</strong> 第 4027 行写的是 <code>==</code>。Widget 默认沿用对象身份比较，因此最直观的命中方式是把同一个 Widget 实例再交回来。两个分别调用非 const 构造函数创建的 <code>SizedBox(width: 80)</code>，即使所有参数相同，通常也会走第 5 条；之后由约束 setter 比较具体值，再决定是否标记布局。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L366">widgets&#x2F;framework.dart:366</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L4027-L4035">widgets&#x2F;framework.dart:4027–4035</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/proxy_box.dart#L226-L233">rendering&#x2F;proxy_box.dart:226–233</a></p><p><strong>第 5 条保留 Element，换上新配置。</strong> <code>Widget.canUpdate</code> 的完整返回条件只有一行。两个 key 都为 null 时，key 比较也成立。宽度、颜色、文字等配置都不参与这个判断。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L382-L384">widgets&#x2F;framework.dart:382–384</a></p><figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">return</span> oldWidget.runtimeType == newWidget.runtimeType &amp;&amp; oldWidget.key == newWidget.key;</span><br></pre></td></tr></table></figure><p>两条复用路径都有 <code>if (child.slot != newSlot)</code>。相同就继续，不同则先调用 <code>updateSlotForChild</code>，把位置变化沿负责挂接 RenderObject 的子链向下传。到 <code>RenderObjectElement.updateSlot</code> 时，祖先可调用 <code>moveRenderObjectChild</code> 移动原有渲染对象。例如带 key 的列表项换序时，父节点的 <code>updateChildren</code> 先匹配旧项，再把选好的旧 Element 与新 slot 传给 <code>updateChild</code>。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L4031-L4038">widgets&#x2F;framework.dart:4031–4038</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L4415-L4427">widgets&#x2F;framework.dart:4415–4427</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L6919-L6926">widgets&#x2F;framework.dart:6919–6926</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L4250-L4273">widgets&#x2F;framework.dart:4250–4273</a></p><h4 id="走一遍宽度-80-→-120：截图里的下一根箭头从哪里来？"><a href="#走一遍宽度-80-→-120：截图里的下一根箭头从哪里来？" class="headerlink" title="走一遍宽度 80 → 120：截图里的下一根箭头从哪里来？"></a>走一遍宽度 80 → 120：截图里的下一根箭头从哪里来？</h4><ol><li>新 Widget 存在，旧 Element 也存在。宽度不同，两次拿到的 SizedBox 配置对象不同，第 4 条跳过。</li><li>两个 Widget 的 <code>runtimeType</code> 都是 <code>SizedBox</code>，key 都是 null，第 5 条成立。第 4050 行调用 <code>child.update(newWidget)</code>。这里的 <code>child</code> 是<strong>承载 SizedBox 的子 Element</strong>，父页面的 StatefulElement 正在调用它。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L4035-L4050">widgets&#x2F;framework.dart:4035–4050</a></li><li>这个子 Element 的实际类型是 <code>SingleChildRenderObjectElement</code>。它的 <code>update</code> 先调用 <code>super.update(newWidget)</code>，进入 <code>RenderObjectElement.update</code>。第 6826 行调用 <code>_performRebuild()</code>，第 6850 行才调用新 SizedBox 的 <code>updateRenderObject(this, renderObject)</code>。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L7131-L7136">widgets&#x2F;framework.dart:7131–7136</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L6819-L6827">widgets&#x2F;framework.dart:6819–6827</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L6850">widgets&#x2F;framework.dart:6850</a></li><li>SizedBox 把新的宽高约束写给<strong>原先的</strong> <code>RenderConstrainedBox</code>。它的 setter 发现约束变了，调用 <code>markNeedsLayout()</code>。回到 <code>SingleChildRenderObjectElement.update</code>，它再对自己的可选 child 调一次 <code>updateChild</code>；这一次才轮到里面的 ColoredBox。最终，外层调用返回的仍是原来承载 SizedBox 的 Element。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/basic.dart#L2808-L2815">widgets&#x2F;basic.dart:2808–2815</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/proxy_box.dart#L226-L233">rendering&#x2F;proxy_box.dart:226–233</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L7135">widgets&#x2F;framework.dart:7135</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L4059">widgets&#x2F;framework.dart:4059</a></li></ol><p>如果第 4050 行的 <code>child</code> 承载的是自定义 <code>StatefulWidget</code>，接下来会进入 <code>StatefulElement.update</code>：保留已有 State，更新其 widget，调用 <code>didUpdateWidget(oldWidget)</code>，再强制 rebuild。若承载 <code>StatelessWidget</code>，则由 <code>StatelessElement.update</code> 更新配置并强制 rebuild。<strong>图三向下连到 RenderObjectElement 的箭头，描述的是本例命中第 5 条并且子 Widget 为 SizedBox 的情况。</strong> <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L5999-L6005">widgets&#x2F;framework.dart:5999–6005</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L6020">widgets&#x2F;framework.dart:6020</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L5905-L5909">widgets&#x2F;framework.dart:5905–5909</a></p><h4 id="停用的-Element-去哪了？inflateWidget-一定创建新-Element-吗？"><a href="#停用的-Element-去哪了？inflateWidget-一定创建新-Element-吗？" class="headerlink" title="停用的 Element 去哪了？inflateWidget 一定创建新 Element 吗？"></a>停用的 Element 去哪了？inflateWidget 一定创建新 Element 吗？</h4><p>第 2、6 条都会调用 <code>deactivateChild</code>。它清掉旧子 Element 的父引用，分离渲染对象，并将子节点放入 inactive 集合。同一帧里这个子树还有机会被接回；到 <code>finalizeTree()</code> 时仍在集合里的节点才被卸载。对于 StatefulElement，卸载才会走到 <code>state.dispose()</code>。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L4645-L4649">widgets&#x2F;framework.dart:4645–4649</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L3352-L3358">widgets&#x2F;framework.dart:3352–3358</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L2118-L2129">widgets&#x2F;framework.dart:2118–2129</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L6043">widgets&#x2F;framework.dart:6043</a></p><p>第 3、6 条都会调用 <code>inflateWidget(newWidget, newSlot)</code>。普通路径执行 <code>newWidget.createElement()</code>，随后 <code>mount(this, newSlot)</code>。如果新 Widget 有 GlobalKey，框架先查这个 key 对应的 Element，并检查 <code>Widget.canUpdate</code>。能取回时，先把它挂回当前父节点，再用 <code>updateChild</code> 更新它，返回取回的 Element；找不到或不兼容才走创建路径。这也解释了第 6 条为什么写“返回 inflateWidget 的结果”。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L4494-L4507">widgets&#x2F;framework.dart:4494–4507</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L4582-L4603">widgets&#x2F;framework.dart:4582–4603</a></p><details><summary>对照完整方法：热重载保护、性能跟踪和 GlobalKey 断言</summary><p>第 4005 行把 <code>hasSameSuperclass</code> 初始化为 true。调试模式在断言里检查旧 Element 和新 Widget 的类别。热重载若把同一个组件从 Stateful 改成 Stateless（或反过来），类别检查可能失败；第 4027、4035 行的两个复用条件都会短路，进入第 6 条。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L4004-L4027">widgets&#x2F;framework.dart:4004–4027</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L4035">widgets&#x2F;framework.dart:4035</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L4060-L4067">widgets&#x2F;framework.dart:4060–4067</a></p><p>第 4039–4053 行按构建性能跟踪开关决定是否在 <code>child.update</code> 前后记录时间线；开启增强调试信息时，还会附加 Widget 的诊断参数。第 4054–4058 行的断言检查更新后的配置并记录调试信息。它们不改变六条路径的返回值。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L4039-L4059">widgets&#x2F;framework.dart:4039–4059</a></p><p>新 Widget 非空时，各路径汇合到方法末尾的调试断言：若传入过旧 child，就清理旧的 GlobalKey 预留记录；若新 key 是 GlobalKey，则为当前父子关系登记预留，供框架检查重复使用。真正凭 GlobalKey 取回节点发生在上面的 <code>inflateWidget</code>。新 Widget 为空时已在第 4000 行返回；其余路径在第 4087 行返回 <code>newChild</code>。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L4075-L4087">widgets&#x2F;framework.dart:4075–4087</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L3356-L3361">widgets&#x2F;framework.dart:3356–3361</a></p></details><p>如果只改 <code>width</code>，<code>SizedBox</code> 把新宽高转成附加约束，setter 比较后调用 <code>markNeedsLayout()</code>。至于屏幕上的宽度是否真的会变，要等 <code>RenderConstrainedBox.performLayout()</code> 把附加约束与父约束合并，再确定尺寸。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/basic.dart#L2808-L2815">widgets&#x2F;basic.dart:2808–2815</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/proxy_box.dart#L226-L233">rendering&#x2F;proxy_box.dart:226–233</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/proxy_box.dart#L293-L301">rendering&#x2F;proxy_box.dart:293–301</a></p><h3 id="Layout-和-paint-的边界各管什么？"><a href="#Layout-和-paint-的边界各管什么？" class="headerlink" title="Layout 和 paint 的边界各管什么？"></a>Layout 和 paint 的边界各管什么？</h3><table><thead><tr><th align="left"></th><th align="left">布局边界（relayout boundary）</th><th align="left">重绘边界（repaint boundary）</th></tr></thead><tbody><tr><td align="left"><strong>什么变化会传到这里？</strong></td><td align="left">节点需要重新计算布局，且父级可能依赖子节点的尺寸。</td><td align="left">节点需要重录绘制内容，当前节点与祖先可能共享绘制层。</td></tr><tr><td align="left"><strong>在哪里停止向上标记？</strong></td><td align="left">从自身开始。框架在 <code>layout</code> 中依据父级是否使用子尺寸、<code>sizedByParent</code>、紧约束或根节点来确定边界。遇到已标脏节点可提前结束。</td><td align="left">从自身开始。已经是重绘边界、且此前也作为边界绘制过的节点可独立入队；否则通常继续通知父级。常规视图的根 <code>RenderView</code> 本身就是重绘边界。</td></tr><tr><td align="left"><strong>队列由谁处理？</strong></td><td align="left">所属 <code>PipelineOwner</code> 持有 <code>_nodesNeedingLayout</code>。<code>flushLayout</code> 从浅到深处理仍脏的节点，常规边界入口是 <code>_layoutWithoutResize()</code>。</td><td align="left">所属 <code>PipelineOwner</code> 持有 <code>_nodesNeedingPaint</code>。<code>flushPaint</code> 从深到浅处理。需要重绘且图层已挂接的边界进入 <code>PaintingContext.repaintCompositedChild</code>。</td></tr></tbody></table><p>布局边界从 <code>performLayout</code> 开始工作。以普通盒布局为例，父节点给子节点传约束、按需调用 <code>child.layout(...)</code>，再根据布局规则决定自己的大小和子节点的位置。子节点相对父节点的偏移通常由父节点写入 <code>child.parentData.offset</code>。完成布局后调用 <code>markNeedsPaint</code>。边界入队时，请求视觉更新的是 <strong><code>PipelineOwner.requestVisualUpdate()</code></strong>；若正处于本帧的布局阶段，调度器不会因此再请求一帧，本帧稍后的 paint 就能处理。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/object.dart#L2676-L2721">布局标记与边界</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/object.dart#L2863-L2883">边界判定</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/object.dart#L1157-L1175">flushLayout</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/object.dart#L2770-L2782">布局结束</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/object.dart#L3338-L3365">绘制标记</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/object.dart#L1068-L1075">请求视觉更新</a></p><h3 id="从绘制指令到屏幕像素"><a href="#从绘制指令到屏幕像素" class="headerlink" title="从绘制指令到屏幕像素"></a>从绘制指令到屏幕像素</h3><ol><li><strong>更新合成标记。</strong> <code>flushCompositingBits</code> 更新脏节点的 <code>needsCompositing</code> 等信息，供随后的绘制决定某些效果是否需要独立的合成层。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/object.dart#L1239-L1254">flushCompositingBits</a></li><li><strong>录制和复用。</strong> 普通子节点可以画进父节点的 <code>PaintingContext</code>；对 Canvas 的绘制操作会被录制为 <code>Picture</code>，交给 <code>PictureLayer</code>。遇到重绘边界时，框架可以把它已有的 <code>OffsetLayer</code> 接回父层。这样，父级重绘时，干净的边界子树可以跳过重新录制。裁剪、变换等绘制操作也可以创建 Layer。Layer 可以跨帧复用，是否缓存为像素是另一层决策。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/object.dart#L249-L292">paintChild 与边界复用</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/object.dart#L359-L410">Picture 录制</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/object.dart#L583-L593">带 Layer 的裁剪</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/object.dart#L1314-L1334">flushPaint</a></li><li><strong>提交场景。</strong> <code>RenderView.compositeFrame</code> 创建 <code>SceneBuilder</code>，用根 Layer 的 <code>buildScene</code> 生成 <code>ui.Scene</code>，再调用 <code>FlutterView.render(scene, ...)</code>。到这里可以确认 Dart 框架已把场景交给引擎。后续由引擎栅格化并通过平台呈现；这次方法调用返回时，无法据此判断屏幕像素已经刷新。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/rendering/view.dart#L347-L363">RenderView.compositeFrame</a></li></ol><p><a id="during-build"></a></p><h2 id="在-build-期间调用-setState-会怎样？"><a href="#在-build-期间调用-setState-会怎样？" class="headerlink" title="在 build 期间调用 setState 会怎样？"></a>在 build 期间调用 setState 会怎样？</h2><p>看<strong>哪个 Element 正在构建，以及要标脏谁</strong>。调试模式下，<code>markNeedsBuild</code> 会检查当前构建目标。祖先正在构建时，标记它的后代有一个允许的分支，因为框架先构建父节点，接下来仍能处理后代。反过来，子节点正在 build 时去标记祖先，或去标记另一分支，通常会触发 <code>setState() or markNeedsBuild() called during build</code>。对正在执行自身 <code>build</code> 的组件，它的 dirty 标志要等 <code>build</code> 返回才清除，在里面调用自己的 <code>setState</code> 也不能用来安排再构建一遍。<a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L5352-L5404">构建期检查</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L3774-L3780">构建目标判断</a> · <a href="https://github.com/flutter/flutter/blob/d3b14c876900e553bc736ca19295fc09e3853e8e/packages/flutter/lib/src/widgets/framework.dart#L5823-L5855">清除 dirty 的时机</a></p>]]>
    </content>
    <id>https://liuduwei-blog.pages.dev/2023/07/15/Flutter-setState-%E6%89%A7%E8%A1%8C%E6%B5%81%E4%B8%8E-updateChild-%E5%85%AD%E6%9D%A1%E8%B7%AF%E5%BE%84/</id>
    <link href="https://liuduwei-blog.pages.dev/2023/07/15/Flutter-setState-%E6%89%A7%E8%A1%8C%E6%B5%81%E4%B8%8E-updateChild-%E5%85%AD%E6%9D%A1%E8%B7%AF%E5%BE%84/"/>
    <published>2023-07-15T02:00:00.000Z</published>
    <summary>
      <![CDATA[<p><a href="#schedule">点击与调度</a> · <a href="#frame">一帧的执行顺序</a> · <a href="#render-object">局部更新</a> · <a href="#during-build">build 期间调用</a></p>
<p>以 Wonderous 使用的 <strong>Flutter 3.47.2</strong>（源码提交 <code>d3b14c8769</code>）为准。先看一个已经显示在屏幕上的页面：用户点击按钮，事件回调执行 <code>setState(() &#123; _selected = true; &#125;)</code>。<strong>回调返回时，<code>_selected</code> 已经变了；屏幕何时反映这个值，取决于框架何时重建该 Element，以及新配置改变了哪些渲染属性。</strong> 本文沿用《Flutter 实战》14.3 的阶段顺序，图中方法名和队列归属对应这个 SDK 版本。<a href="https://book.flutterchina.club/chapter14/flutter_app_startup.html">原文章节</a> · <a href="https://api.flutter.dev/flutter/widgets/State/setState.html">State.setState 官方说明</a></p>]]>
    </summary>
    <title>Flutter 的 setState 执行流与 updateChild 六条路径</title>
    <updated>2026-12-01T15:40:31.640Z</updated>
  </entry>
  <entry>
    <author>
      <name>Leo Liu</name>
    </author>
    <category term="Javascript" scheme="https://liuduwei-blog.pages.dev/tags/Javascript/"/>
    <category term="Algorithm" scheme="https://liuduwei-blog.pages.dev/tags/Algorithm/"/>
    <category term="Sort" scheme="https://liuduwei-blog.pages.dev/tags/Sort/"/>
    <content>
      <![CDATA[<p>Elementary Sort, The slowest and simplest Sort algorithm, time complexity is O(n^2)</p><h2 id="Bubble-Sort"><a href="#Bubble-Sort" class="headerlink" title="Bubble Sort"></a>Bubble Sort</h2><p>As Its name, the bigger element will be like a heavy item, Sinking to the bottom of the water, and the small one will swim up to the surface</p><p>Here’s the algorithm animation: <a href="https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html">Bubble Sort</a></p><p>javascript code:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">function bubbleSort(a) &#123;</span><br><span class="line">  for (let i = a.length - 1; i &gt;= 0; i--) &#123;</span><br><span class="line">    for (let j = 0; j &lt; i; j++) &#123;</span><br><span class="line">      if (a[j] &lt; a[j + 1]) &#123;</span><br><span class="line">        exchange(a, j, j + 1);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function exchange(a, i, j) &#123;</span><br><span class="line">  var temp = a[i];</span><br><span class="line">  a[i] = a[j];</span><br><span class="line">  a[j] = temp;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">module.exports = bubbleSort;</span><br></pre></td></tr></table></figure><h2 id="Insertion-Sort"><a href="#Insertion-Sort" class="headerlink" title="Insertion Sort"></a>Insertion Sort</h2><p>loop through elements, every time counter new element Insert it into already ordered element in front of it.</p><p>javascript code:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">function insertionSort(a, low = 0, high = a.length - 1) &#123;</span><br><span class="line">  for (let i = low; i &lt;= high; i++) &#123;</span><br><span class="line">    for (let j = i; j &gt; low &amp;&amp; less(a[j], a[j - 1]); j--) &#123;</span><br><span class="line">      exchange(a, j, j - 1)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function exchange()&#123;...see before&#125;</span><br><span class="line"></span><br><span class="line">function less(a, b) &#123;</span><br><span class="line">  return a &lt; b ? true : false;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h2 id="Selection-Sort"><a href="#Selection-Sort" class="headerlink" title="Selection Sort"></a>Selection Sort</h2><p>Loop through elements, choose the smallest one, and put it into the front of elements every round.</p><p>javascript code:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">function selectionSort(a) &#123;</span><br><span class="line">  var length = a.length;</span><br><span class="line">  for (let i = 0; i &lt; length; i++) &#123;</span><br><span class="line">    let min = i;</span><br><span class="line">    // find min</span><br><span class="line">    for (let j = i; j &lt; length; j++) &#123;</span><br><span class="line">      if (less(a[j], a[min])) &#123;</span><br><span class="line">        min = j;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    // exchange</span><br><span class="line">    if (a[min] != a[i]) &#123;</span><br><span class="line">      let temp = a[i];</span><br><span class="line">      a[i] = a[min];</span><br><span class="line">      a[min] = temp;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">module.exports = selectionSort;</span><br></pre></td></tr></table></figure><h2 id="Conclusion"><a href="#Conclusion" class="headerlink" title="Conclusion"></a>Conclusion</h2><p>There may be other optimizations for these three algorithms, but  I just want to talk about the basic version in this post, other optimizations can be discussed in new posts.</p><p>Next, I want to talk about Shell Sort, I treat it as Advanced Selection Sort, It’s really amazing in real-world production, tiny easy code but effective</p>]]>
    </content>
    <id>https://liuduwei-blog.pages.dev/2023/01/23/Js%E5%AE%9E%E7%8E%B0%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95%EF%BC%881%EF%BC%89%EF%BC%9A%E5%9F%BA%E7%A1%80%E6%8E%92%E5%BA%8F-%E9%80%89%E6%8B%A9%EF%BC%8C%E6%8F%92%E5%85%A5%E6%8E%92%E5%BA%8F/</id>
    <link href="https://liuduwei-blog.pages.dev/2023/01/23/Js%E5%AE%9E%E7%8E%B0%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95%EF%BC%881%EF%BC%89%EF%BC%9A%E5%9F%BA%E7%A1%80%E6%8E%92%E5%BA%8F-%E9%80%89%E6%8B%A9%EF%BC%8C%E6%8F%92%E5%85%A5%E6%8E%92%E5%BA%8F/"/>
    <published>2023-01-23T08:37:13.000Z</published>
    <summary>
      <![CDATA[<p>Elementary Sort, The slowest and simplest Sort algorithm, time complexity is O(n^2)</p>
<h2 id="Bubble-Sort"><a href="#Bubble-Sort" class]]>
    </summary>
    <title>Sort Algorithm（1）： Elementary Sort Javascript Implement - Selection， Insertion， Bubble Sort</title>
    <updated>2026-12-01T15:40:31.640Z</updated>
  </entry>
  <entry>
    <author>
      <name>Leo Liu</name>
    </author>
    <category term="Javascript" scheme="https://liuduwei-blog.pages.dev/tags/Javascript/"/>
    <content>
      <![CDATA[<figure class="highlight plaintext"><figcaption><span>these three functions are very useful in everyday development, What exactly did they do? Let's break it down.</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">## call</span><br><span class="line">use case:</span><br></pre></td></tr></table></figure><p>var person &#x3D; {<br>  name: “leo”,<br>  sayName(greetings) {<br>    console.log(greetings, this.name);<br>  },<br>};</p><p>var anotherPerson &#x3D; {<br>  name: “norris”,<br>};</p><p>person.sayName.call(anotherPerson, “hi”);</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">What ```function.call(object,...params)``` do:</span><br><span class="line"></span><br><span class="line">1. point this to the object passed in by first parameter</span><br><span class="line">2. invoke function in context of object</span><br><span class="line"></span><br><span class="line">Here is our versoin of ```call```:</span><br><span class="line"></span><br></pre></td></tr></table></figure><p>function myCall(object, …params) {<br>  var o &#x3D; object || window;<br>  var fn &#x3D; Symbol(“fn”);<br>  o[fn] &#x3D; this;<br>  o<a href="...params">fn</a>;<br>  delete o[fn];<br>}</p><p>Function.prototype.myCall &#x3D; myCall;</p><p>var person &#x3D; {<br>  name: “leo”,<br>  sayName(greetings) {<br>    console.log(greetings, this.name);<br>  },<br>};</p><p>var anotherPerson &#x3D; {<br>  name: “norris”,<br>};</p><p>person.sayName.myCall(anotherPerson, “hi”);</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">## Apply</span><br><span class="line">It&#x27;s almost exactly the same as the call, The only difference is in the second argument of the function.</span><br><span class="line"></span><br><span class="line">&gt; Note: This function is almost identical to apply(), except that the function arguments are passed to call() individually as a list, while for apply() they are combined in one object, typically an array — for example, func.call(this, &quot;eat&quot;, &quot;bananas&quot;) vs. func.apply(this, [&quot;eat&quot;, &quot;bananas&quot;]).</span><br><span class="line"></span><br><span class="line">Here is our version of Apply:</span><br><span class="line"></span><br></pre></td></tr></table></figure><p>function myApply(object, args) {<br>  var o &#x3D; object || window;<br>  var fn &#x3D; Symbol(“fn”);<br>  o[fn] &#x3D; this;<br>  if (Array.isArray(args)) {<br>    o<a href="...args">fn</a>;<br>  } else {<br>    o<a href="">fn</a>;<br>  }<br>  delete o[fn];<br>}</p><p>Function.prototype.myApply &#x3D; myApply;</p><p>var person &#x3D; {<br>  name: “leo”,<br>  sayName(greetings) {<br>    console.log(greetings, this.name);<br>  },<br>};</p><p>var anotherPerson &#x3D; {<br>  name: “norris”,<br>};</p><p>person.sayName.myApply(anotherPerson, [“hi”]);</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">## bind</span><br><span class="line">```Function.bind(object, ...args)``` do:</span><br><span class="line">1. return a function that, when executed, executes the target function, with the target&#x27;s this always pointing to the target object.\</span><br><span class="line"></span><br><span class="line">code:</span><br><span class="line"></span><br></pre></td></tr></table></figure><p>function myBind(object, …args1) {<br>  var fn &#x3D; this;<br>  return function f(…args2) {<br>    var args &#x3D; args1.concat(args2);<br>    fn.call(object, …args);<br>  };<br>}</p><p>Function.prototype.myBind &#x3D; myBind;</p><p>var person &#x3D; {<br>  name: “leo”,<br>  sayName(greetings) {<br>    console.log(greetings, this.name);<br>  },<br>};</p><p>var anotherPerson &#x3D; {<br>  name: “norris”,<br>};</p><p>var f &#x3D; person.sayName.myBind(anotherPerson, “hi”);<br>f();<br>&#96;&#96;&#96;</p>]]>
    </content>
    <id>https://liuduwei-blog.pages.dev/2022/10/23/Implement-Your-Own-Call-Bind-Apply/</id>
    <link href="https://liuduwei-blog.pages.dev/2022/10/23/Implement-Your-Own-Call-Bind-Apply/"/>
    <published>2022-10-23T05:39:53.000Z</published>
    <summary>
      <![CDATA[<figure class="highlight plaintext"><figcaption><span>these three functions are very useful in everyday development, What exactly did they d]]>
    </summary>
    <title>Implement Your Own Call, Bind, Apply</title>
    <updated>2026-12-01T15:40:31.640Z</updated>
  </entry>
  <entry>
    <author>
      <name>Leo Liu</name>
    </author>
    <category term="Javascript" scheme="https://liuduwei-blog.pages.dev/tags/Javascript/"/>
    <category term="Object" scheme="https://liuduwei-blog.pages.dev/tags/Object/"/>
    <category term="Prototype" scheme="https://liuduwei-blog.pages.dev/tags/Prototype/"/>
    <content>
      <![CDATA[<h2 id="prelude"><a href="#prelude" class="headerlink" title="prelude"></a>prelude</h2><p>Ever since I was introduced to the programming language javascript, Its prototype chain has always bothered me,<br>I read a lot of blog, posts about this topic, most of them say the same thing, Put a picture like this in the article,</p><p><img src="/../images/JavascriptObjectHierarchy.jpg" alt="prototype chain"></p><p>and told readers to remember :</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">1. People.prototype.constructor == Person </span><br><span class="line">2. PeopleLeo.__proto__ == Person.prototype </span><br></pre></td></tr></table></figure><p>it’s not wrong, but it’s just too hard to keep in mind for me, I memorize then forgot again and again, and every time I try to recall it, I was thinking what fuck is the <code>prototype</code> or <code>__proto__</code>, they look like the same thing, who figure out these two names, why javascript use this mechanism to simulate class inheritance.</p><p>And now I know how this came to be, It dawned on me, after I read this <a href="https://blog.vjeux.com/2011/javascript/how-prototypal-inheritance-really-works.html">Javascript – How Prototypal Inheritance really works</a><br>I highly recommend you read this post.</p><h2 id="Prototypal-inheritance"><a href="#Prototypal-inheritance" class="headerlink" title="Prototypal inheritance"></a>Prototypal inheritance</h2><p>for me, the most important thing I learned from <a href="https://blog.vjeux.com/2011/javascript/how-prototypal-inheritance-really-works.html">Javascript – How Prototypal Inheritance really works</a>,<br>javascript’s Inheritance is just prototypal Inheritance, It has nothing to do with class Inheritance, the <code>new</code> key word in javascript it’s because:</p><blockquote><p><a href="https://brendaneich.com/">Brendan Eich</a> wanted Javascript to look like traditional Object Oriented programming languages such as Java and C++. In those, we use the new operator to make a new instance of a class. So he wrote a new operator for Javascript. (quote from <a href="https://blog.vjeux.com/2011/javascript/how-prototypal-inheritance-really-works.html">https://blog.vjeux.com/2011/javascript/how-prototypal-inheritance-really-works.html</a>)</p></blockquote><p>if you came from other traditional Object Oriented programming languages that use class inheritance, when you try to understand the javascript prototypal inheritance, you will suck.</p><p>so embrace the prototypal inheritance, feel its beauty, and think it without class inheritance, all things come to be ease.</p><h2 id="some-code-example"><a href="#some-code-example" class="headerlink" title="some code example"></a>some code example</h2><p>After I realized what I learned from <a href="https://blog.vjeux.com/2011/javascript/how-prototypal-inheritance-really-works.html">Javascript – How Prototypal Inheritance really works</a>,  I was suddenly reminded of an online class I’ve taken before, it’s <a href="https://frontendmasters.com/courses/deep-javascript-v3/">deep-javascript</a> taught by Kyle Simpson, author of <a href="https://github.com/getify/You-Dont-Know-JS">You don’t know Js</a></p><p>it also mentions the concept of prototypal inheritance, called OLOO: Objects Linked to Other Objects.</p><p>Here’s some code I noted, see how prototypal inheritance works:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">function Workshop(teacher) &#123;</span><br><span class="line">  this.teacher = teacher;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Workshop.prototype.ask = function (question) &#123;</span><br><span class="line">  console.log(this.teacher + question);</span><br><span class="line">&#125;;</span><br><span class="line">var deepJs = new Workshop(&quot;liu&quot;); // not true prototypal inheritance, it&#x27;s a simulation of class inheritance</span><br><span class="line"></span><br><span class="line">function AnotherWorkShop(teacher) &#123;</span><br><span class="line">  Workshop.call(this, teacher);</span><br><span class="line">&#125;</span><br><span class="line">// true prototypal inheritance</span><br><span class="line">AnotherWorkShop.prototype = Object.create(Workshop.prototype);</span><br><span class="line">AnotherWorkShop.prototype.speakUp = function (msg) &#123;</span><br><span class="line">  this.ask(msg.toUpperCase());</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">var reactJs = new AnotherWorkShop(&quot;leo&quot;);</span><br><span class="line">reactJs.speakUp(&quot;hi&quot;);</span><br></pre></td></tr></table></figure><p>and clean version</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">var Workshop = &#123;</span><br><span class="line">  setTeacher(teacher) &#123;</span><br><span class="line">    this.teacher = teacher;</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  ask(msg) &#123;</span><br><span class="line">    console.log(this.teacher, msg);</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">var AnotherWorkShop = Object.assign(Object.create(Workshop), &#123;</span><br><span class="line">  speakUp(msg) &#123;</span><br><span class="line">    this.ask(msg);</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">var recentJs = Object.create(AnotherWorkShop);</span><br><span class="line">recentJs.setTeacher(&quot;leo&quot;);</span><br><span class="line">recentJs.speakUp(&quot;delegation inheritance&quot;);</span><br></pre></td></tr></table></figure><p>use es6 class syntax suger: </p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">class Workshop &#123;</span><br><span class="line">  constructor(teacher) &#123;</span><br><span class="line">    this.teacher = teacher;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  ask(msg) &#123;</span><br><span class="line">    console.log(this.teacher, msg);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">class AnotherWorkShop extends Workshop &#123;</span><br><span class="line">  speakUp(msg) &#123;</span><br><span class="line">    this.ask(msg);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">var jsRecentParts = new AnotherWorkShop(&quot;kyle&quot;);</span><br><span class="line"></span><br><span class="line">jsRecentParts.speakUp(&quot;hi&quot;);</span><br></pre></td></tr></table></figure><h2 id="conclude"><a href="#conclude" class="headerlink" title="conclude"></a>conclude</h2><p>let’s see this picture again,</p><p><img src="/../images/JavascriptObjectHierarchy.jpg" alt="prototype chain"> </p><p>Is everything clear and unambiguous?</p><h2 id="Reference"><a href="#Reference" class="headerlink" title="Reference"></a>Reference</h2><p><a href="http://www.mollypages.org/tutorials/js.mp">Javascript Object Layout</a><br><a href="https://www.ruanyifeng.com/blog/2011/06/designing_ideas_of_inheritance_mechanism_in_javascript.html">Javascript继承机制的设计思想</a><br><a href="https://frontendmasters.com/teachers/kyle-simpson/">deep-javascript</a><br><a href="https://crockford.com/javascript/prototypal.html">Prototypal Inheritance in JavaScript</a></p>]]>
    </content>
    <id>https://liuduwei-blog.pages.dev/2022/09/29/Understand-Javscript-Prototype-chain-Prototypal-Inheritance/</id>
    <link href="https://liuduwei-blog.pages.dev/2022/09/29/Understand-Javscript-Prototype-chain-Prototypal-Inheritance/"/>
    <published>2022-09-29T03:39:29.000Z</published>
    <summary>
      <![CDATA[<h2 id="prelude"><a href="#prelude" class="headerlink" title="prelude"></a>prelude</h2><p>Ever since I was introduced to the programming lan]]>
    </summary>
    <title>Understand Javascript Prototype chain - Prototypal Inheritance</title>
    <updated>2026-12-01T15:40:31.640Z</updated>
  </entry>
  <entry>
    <author>
      <name>Leo Liu</name>
    </author>
    <category term="Javascript" scheme="https://liuduwei-blog.pages.dev/tags/Javascript/"/>
    <category term="Object" scheme="https://liuduwei-blog.pages.dev/tags/Object/"/>
    <content>
      <![CDATA[<p>We can use both <code>new</code> and Object.create() implement inherantance in javascript:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">// new</span><br><span class="line">function People(name) &#123;</span><br><span class="line">  this.name = name;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">People.prototype.sayName = function () &#123;</span><br><span class="line">  console.log(this.name);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">var leo = new People(&quot;leo&quot;);</span><br><span class="line"></span><br><span class="line">// object.create()</span><br><span class="line">var People = &#123;</span><br><span class="line">  name: &quot;liu&quot;,</span><br><span class="line">  sayName() &#123;</span><br><span class="line">    console.log(this.name);</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">var norris = Object.create(People);</span><br><span class="line">norris.name = &quot;norris&quot;;</span><br><span class="line">norris.sayName();</span><br></pre></td></tr></table></figure><p>we can implement our new and Object.create():</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">// my new</span><br><span class="line">function myNew(constructor, ...params) &#123;</span><br><span class="line">  var o = &#123;&#125;;</span><br><span class="line">  Object.setPrototypeOf(o, constructor.prototype);</span><br><span class="line">  var result = constructor.apply(o, params);</span><br><span class="line">  return result instanceof o ? result : o;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">//my Obejct.create</span><br><span class="line">if (Object.create == null) &#123;</span><br><span class="line">  Object.create = function (parent) &#123;</span><br><span class="line">    function F() &#123;&#125;</span><br><span class="line">    F.prototype = parent;</span><br><span class="line">    return new F();</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>]]>
    </content>
    <id>https://liuduwei-blog.pages.dev/2022/08/22/What-Did-new-Keyword-Object-create-Do-In-Javascript/</id>
    <link href="https://liuduwei-blog.pages.dev/2022/08/22/What-Did-new-Keyword-Object-create-Do-In-Javascript/"/>
    <published>2022-08-22T06:34:19.000Z</published>
    <summary>
      <![CDATA[<p>We can use both <code>new</code> and Object.create() implement inherantance in javascript:</p>
<figure class="highlight plaintext"><table]]>
    </summary>
    <title>What Did new Keyword, Object.create() Do In Javascript</title>
    <updated>2026-12-01T15:40:31.640Z</updated>
  </entry>
  <entry>
    <author>
      <name>Leo Liu</name>
    </author>
    <category term="Javascript" scheme="https://liuduwei-blog.pages.dev/tags/Javascript/"/>
    <category term="Object" scheme="https://liuduwei-blog.pages.dev/tags/Object/"/>
    <content>
      <![CDATA[<p>Loop through an Object is much harder than doing the same thing in an Array because you can’t use those convenient syntaxes like for-of, forEach (by the way, you should never use for-in) when dealing with an Object, so today let’s look at how many methods are there to loop through an Object</p><h2 id="Create-an-object-to-test"><a href="#Create-an-object-to-test" class="headerlink" title="Create an object to test"></a>Create an object to test</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">function Ob() &#123;</span><br><span class="line">  this.name = &quot;liu&quot;;</span><br><span class="line">  this.age = 22;</span><br><span class="line">&#125;</span><br><span class="line">var ob = new Ob();</span><br><span class="line"></span><br><span class="line">Object.defineProperty(ob, &quot;gender&quot;, &#123;</span><br><span class="line">  value: &quot;male&quot;,</span><br><span class="line">  enumerable: false,</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">var mySb = Symbol(&quot;special&quot;);</span><br><span class="line"></span><br><span class="line">ob[mySb] = &quot;sp&quot;;</span><br><span class="line"></span><br><span class="line">var mySbEnumerableFalse = Symbol(&quot;emfalse&quot;);</span><br><span class="line">Object.defineProperty(ob, mySbEnumerableFalse, &#123;</span><br><span class="line">  value: &quot;emfalse:value&quot;,</span><br><span class="line">  enumerable: false,</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">Ob.prototype.father = &quot;leo&quot;;</span><br></pre></td></tr></table></figure><p>console log this in Node:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">console.log(ob);</span><br><span class="line"></span><br><span class="line">output:</span><br><span class="line">Ob &#123; name: &#x27;liu&#x27;, age: 22, [Symbol(special)]: &#x27;sp&#x27; &#125;</span><br></pre></td></tr></table></figure><p>notice,console.log(Object) will only output own enumerable properties.</p><h2 id="Object-keys-Object-Object-Values-Object-entries"><a href="#Object-keys-Object-Object-Values-Object-entries" class="headerlink" title="Object.keys(Object)(Object.Values(); Object.entries())"></a>Object.keys(Object)(Object.Values(); Object.entries())</h2><p>those methods above will return an array including the parameter Object’s keys or values or [key, value] pairs, here’s the code:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">console.log(Object.values(object)); // ignore enumerable: false </span><br><span class="line">console.log(Object.keys(object)); // ignore enumerable: false </span><br><span class="line">console.log(Object.entries(object)); // ignore same</span><br></pre></td></tr></table></figure><p>output here:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[ &#x27;liu&#x27;, 22 ] // Object.values(object)</span><br><span class="line">[ &#x27;name&#x27;, &#x27;age&#x27; ] // Object.keys(object)</span><br><span class="line">[ [ &#x27;name&#x27;, &#x27;liu&#x27; ], [ &#x27;age&#x27;, 22 ] ] // Object.entries(object)</span><br></pre></td></tr></table></figure><p>so these three methods only count their own enumerable none symbol properties in</p><h2 id="Object-getOwnPropertyNames（getOwnPropertySymbols-ob-）"><a href="#Object-getOwnPropertyNames（getOwnPropertySymbols-ob-）" class="headerlink" title="Object.getOwnPropertyNames（getOwnPropertySymbols(ob)）"></a>Object.getOwnPropertyNames（getOwnPropertySymbols(ob)）</h2><p>directly look at the code:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">console.log(Object.getOwnPropertyNames(ob)); // enumerable: false would still in output array [names....], but no symbol keys</span><br><span class="line">console.log(Object.getOwnPropertySymbols(ob)); // enumerable: false would still in output array [names....], only symbol keys</span><br></pre></td></tr></table></figure><p>output:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[ &#x27;name&#x27;, &#x27;age&#x27;, &#x27;gender&#x27; ]</span><br><span class="line">[ Symbol(special), Symbol(emfalse) ]</span><br></pre></td></tr></table></figure><p>The two are complementary, and will output all own properties but not include ancestors’, no matter how the enumerable.</p><h2 id="Reflect-ownKeys"><a href="#Reflect-ownKeys" class="headerlink" title="Reflect.ownKeys()"></a>Reflect.ownKeys()</h2><p>it’s do the same thing as <code>Object.getOwnPropertyNames()</code> and <code>Object.getOwnPropertySymbols(ob)</code><br>code here:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">console.log(Reflect.ownKeys(ob));</span><br><span class="line"></span><br><span class="line">output:</span><br><span class="line">[ &#x27;name&#x27;, &#x27;age&#x27;, &#x27;gender&#x27;, Symbol(special), Symbol(emfalse) ]</span><br></pre></td></tr></table></figure><h2 id="summary"><a href="#summary" class="headerlink" title="summary"></a>summary</h2><p>which methods to use it’s a subjective topic, it’s up to your real code context, and I like the use <code>Reflect.ownKeys()</code> , but it’s only available in es6, so i will contact <code>Object.getOwnPropertyNames()</code> and <code>Object.getOwnPropertySymbols(ob)</code></p><h2 id="Reference-List"><a href="#Reference-List" class="headerlink" title="Reference List"></a>Reference List</h2><p><a href="https://juejin.cn/post/7129374520015585317">JS遍历对象的七种方法</a></p>]]>
    </content>
    <id>https://liuduwei-blog.pages.dev/2022/08/19/You-Must-Know-About-Object-How-To-Loop-Through-An-Object-In-JavaScript/</id>
    <link href="https://liuduwei-blog.pages.dev/2022/08/19/You-Must-Know-About-Object-How-To-Loop-Through-An-Object-In-JavaScript/"/>
    <published>2022-08-19T12:58:56.000Z</published>
    <summary>
      <![CDATA[<p>Loop through an Object is much harder than doing the same thing in an Array because you can’t use those convenient syntaxes like for-of,]]>
    </summary>
    <title>You Must Know About Object - How To Loop Through An Object In JavaScript</title>
    <updated>2026-12-01T15:40:31.640Z</updated>
  </entry>
  <entry>
    <author>
      <name>Leo Liu</name>
    </author>
    <category term="Api" scheme="https://liuduwei-blog.pages.dev/tags/Api/"/>
    <category term="WebDevelopment" scheme="https://liuduwei-blog.pages.dev/tags/WebDevelopment/"/>
    <content>
      <![CDATA[<blockquote><p>I believe many newbie to web Developer have these questions, so  let’s talk about it and fiugre out the exact details of these API Achitectures(or specifics)</p></blockquote><h2 id="RPC-Remote-Procedure-Call"><a href="#RPC-Remote-Procedure-Call" class="headerlink" title="RPC (Remote Procedure Call)"></a>RPC (Remote Procedure Call)</h2><blockquote><p>In distributed computing, a remote procedure call (RPC) is when a computer program causes a procedure (subroutine) to execute in a different address space (commonly on another computer on a shared network), which is written as if it were a normal (local) procedure call, without the programmer explicitly writing the details for the remote interaction. That is, the programmer writes essentially the same code whether the subroutine is local to the executing program, or remote.<br>—Wikipedia</p></blockquote><p>Actually RPC originally refers to communication in computer process, But in our context, RPC is a sytle of API design that focus the actions.<br>It can be confusing, let me explain. For example, if we want to fetch a movie list using the <code>Get</code> method with API address <code>api/getMovieList</code>, it’s an RPC style API, it’s an action that retrieves the movie list.</p><p>let’s see some more RPC APIS:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">// Add new product</span><br><span class="line">POST /addProduct HTTP/1.1</span><br><span class="line"></span><br><span class="line">HOST: api.example.com</span><br><span class="line"></span><br><span class="line">Content-Type: application/json</span><br><span class="line"></span><br><span class="line">&#123;&quot;name&quot;: &quot;T-Shirt&quot;, &quot;price&quot;: &quot;22.00&quot;, &quot;category&quot;: &quot;Clothes&quot;&#125;</span><br><span class="line"></span><br><span class="line">// get products</span><br><span class="line">POST /getProduct HTTP/1.1</span><br><span class="line"></span><br><span class="line">HOST: api.example.com</span><br><span class="line"></span><br><span class="line">Content-Type: application/json</span><br><span class="line"></span><br><span class="line">&#123;&quot;productID&quot;: &quot;123”&#125;</span><br><span class="line"></span><br><span class="line">// update product</span><br><span class="line">POST /updateProductPrice HTTP/1.1</span><br><span class="line"></span><br><span class="line">HOST: api.example.com</span><br><span class="line"></span><br><span class="line">Content-Type: application/json</span><br><span class="line"></span><br><span class="line">&#123;&quot;productId&quot;: &quot;123&quot;, &quot;newPrice&quot;: &quot;20.00&quot;&#125;</span><br><span class="line"></span><br><span class="line">// delete product</span><br><span class="line">POST /deleteProduct HTTP/1.1</span><br><span class="line"></span><br><span class="line">HOST: api.example.com</span><br><span class="line"></span><br><span class="line">Content-Type: application/json</span><br><span class="line"></span><br><span class="line">&#123;&quot;productId&quot;: &quot;123&quot;&quot;&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">// it&#x27;s json-RPC, RPC also can implemented with XML, but </span><br><span class="line">// Nowdays, it&#x27;s all about using json </span><br></pre></td></tr></table></figure><p>It’s an action or a procedure that remotely calls the server’s function to achive the goal.<br>We use <code>Get</code> when we fetch data, and in other case we use <code>Post</code> and put payload in the <code>body</code>.</p><h2 id="REST-API-representational-state-transfer"><a href="#REST-API-representational-state-transfer" class="headerlink" title="REST API (representational state transfer)"></a>REST API (representational state transfer)</h2><p>REST API have a lot of <a href="https://en.wikipedia.org/wiki/REST">Architectural constraints</a>, Here, I think the main concept of REST is resources.</p><p>First, let’s look at these product APIs mentioned in chapter RPC but in RESTful:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">// Add</span><br><span class="line">POST /products HTTP/1.1</span><br><span class="line"></span><br><span class="line">HOST: api.example.com</span><br><span class="line"></span><br><span class="line">Content-Type: application/json</span><br><span class="line"></span><br><span class="line">&#123;&quot;name&quot;: &quot;T-Shirt&quot;, &quot;price&quot;: &quot;22.00&quot;, &quot;category&quot;: &quot;Clothes&quot;&#125;</span><br><span class="line"></span><br><span class="line">// Get</span><br><span class="line">GET /products/123 HTTP/1.1</span><br><span class="line"></span><br><span class="line">HOST: api.example.com</span><br><span class="line"></span><br><span class="line">// Updated </span><br><span class="line">PUT /products/123 HTTP/1.1</span><br><span class="line"></span><br><span class="line">HOST: api.example.com</span><br><span class="line"></span><br><span class="line">Content-Type: application/json</span><br><span class="line"></span><br><span class="line">&#123;&quot;price&quot;: &quot;20.00&quot;&#125;</span><br><span class="line"></span><br><span class="line">// Delete</span><br><span class="line">DELETE /products/123 HTTP/1.1</span><br><span class="line"></span><br><span class="line">HOST: api.example.com</span><br></pre></td></tr></table></figure><p>It treats product as a resource and use four http methods <code>GET | POST | PUT | DELETE</code> to perform CRUD oprations on it.</p><p>SO, if you know a URL  <code>api/product</code> provides REST API, you should know the corresponding CRUD API URLs.</p><p>But what about other situations apart from CRUD in our business logic, like starting, stopping, restarting a server? I think it’s better use RPC API, REST is hard to describe those things,<br>it excels in CRUD operations on resources, but if you want describe other actions, you would need to spend a lot of time coming up with a REST API.</p><h2 id="GraphQL"><a href="#GraphQL" class="headerlink" title="GraphQL"></a>GraphQL</h2><p>it’s a query language(syntax) via API call which originated in facebook, it’s used specifically for querying data, with the data orginized in a hierarchical structure similar to a tree is better</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">query &#123;</span><br><span class="line">  viewer &#123;</span><br><span class="line">    login</span><br><span class="line">    id</span><br><span class="line">    avatarUrl</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>this screenshot more clear<br><img src="/../images/graphql.jpg" alt="GraphQL"></p><p>I have never worked with GraphQL before. However, i believe that if there a demend to fetch a large amount of structure Data, such as in a social Network, it would be better to use GraphQL.</p>]]>
    </content>
    <id>https://liuduwei-blog.pages.dev/2022/07/28/PRC-REST-GraphQL-What-Are-These-API-Achitectures/</id>
    <link href="https://liuduwei-blog.pages.dev/2022/07/28/PRC-REST-GraphQL-What-Are-These-API-Achitectures/"/>
    <published>2022-07-28T04:02:49.000Z</published>
    <summary>
      <![CDATA[<blockquote>
<p>I believe many newbie to web Developer have these questions, so  let’s talk about it and fiugre out the exact details of the]]>
    </summary>
    <title>RPC, REST, GraphQL - What Are These API Achitectures</title>
    <updated>2026-12-01T15:40:31.640Z</updated>
  </entry>
  <entry>
    <author>
      <name>Leo Liu</name>
    </author>
    <category term="Javascript" scheme="https://liuduwei-blog.pages.dev/tags/Javascript/"/>
    <content>
      <![CDATA[<p>We’ve all written simple for loops to iterate over arrays and objects countless times. But have you ever stopped to consider how the scope of variables used inside loops works? I recently came across an interesting code snippet that highlighted an important distinction between var and let in this context.</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">3</span>; i++) &#123;</span><br><span class="line">  <span class="keyword">const</span> <span class="title function_">log</span> = (<span class="params"></span>) =&gt; <span class="variable language_">console</span>.<span class="title function_">log</span>(i);</span><br><span class="line">  <span class="built_in">setTimeout</span>(log, <span class="number">100</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>When running this, you might expect it to log the numbers 0, 1, 2 - one for each iteration of the loop. But it actually logs 3, 3, 3!</p><p>Why is that? It has to do with how var declarations are scoped. Variables defined with var have function scope, meaning they are accessible outside of any blocks like if statements or for loops.</p><p>In this example, i is declared with var at the start of the for loop. Each iteration increments it and passes the reference to i into the timeout callback<br>If we change var to let:</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; <span class="number">3</span>; i++) &#123;</span><br><span class="line">  <span class="comment">//...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>Now it logs 0, 1, 2 as expected. let bindings are block scoped, so i is treated as a new variable each iteration rather than a single changing reference.</p><p>Moral of the story? Be mindful of scope differences when using loops - it can subtly alter how your code behaves over time. Stick with let if you want block scoping within loops.</p>]]>
    </content>
    <id>https://liuduwei-blog.pages.dev/2022/06/17/var-let-what-s-difference-in-the-for-loop/</id>
    <link href="https://liuduwei-blog.pages.dev/2022/06/17/var-let-what-s-difference-in-the-for-loop/"/>
    <published>2022-06-17T08:36:40.000Z</published>
    <summary>
      <![CDATA[<p>We’ve all written simple for loops to iterate over arrays and objects countless times. But have you ever stopped to consider how the scop]]>
    </summary>
    <title>Var, Let - What's Difference In The For Loop</title>
    <updated>2026-12-01T15:40:31.640Z</updated>
  </entry>
  <entry>
    <author>
      <name>Leo Liu</name>
    </author>
    <category term="Javascript" scheme="https://liuduwei-blog.pages.dev/tags/Javascript/"/>
    <content>
      <![CDATA[<blockquote><p>Recently I came across an article call <a href="https://github.com/myshov/history-of-javascript/tree/master/4_evolution_of_js_modularity">The Evolution of JavaScript Modularity</a> from a github Repo, This article is so well-written, thus this is kind of a summary of that article.</p></blockquote><h3 id="The-Name-Collision"><a href="#The-Name-Collision" class="headerlink" title="The Name Collision"></a>The Name Collision</h3><p>From the moment of its appearance JavaScript has used the global object window as a storage for all variables defined without the <code>var</code> keyword. In 1995-1999 it was very convenient, because JavaScript code tended to solve small tasks that hadn’t required a lot of lines of code. But when the codebase of applications had became large this feature of the language began to lead to nasty errors because of the name collisions. Let’s look at this example:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">// file greeting.js</span><br><span class="line">var helloInLang = &#123;</span><br><span class="line">   en: &#x27;Hello world!&#x27;,</span><br><span class="line">   es: &#x27;¡Hola mundo!&#x27;,</span><br><span class="line">   ru: &#x27;Привет мир!&#x27;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">function writeHello(lang) &#123;</span><br><span class="line">   document.write(helloInLang[lang]);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">// file hello.js</span><br><span class="line">function writeHello() &#123;</span><br><span class="line">   document.write(&#x27;The script is broken&#x27;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>When we place the script <code>greeting.js</code> on the page and after it <code>hello.js</code> there will be conflict, that is instead of the greeting we will get the message “The script is broken” in this particular case.</p><p>It is obvious that in the large projects this can cause a lot of headaches. Moreover you cannot be sure that the third-party scripts on the page won’t break anything in your app.</p><p>And there’s an another tedious moment that you need manual control of the <code>script</code> tag in the HTML.</p><h3 id="Directly-Defined-Dependencies-1999"><a href="#Directly-Defined-Dependencies-1999" class="headerlink" title="Directly Defined Dependencies (1999)"></a>Directly Defined Dependencies (1999)</h3><p>let’s look at the implementation of this pattern using Dojo Toolkit.</p><p>The gist of directly defined dependencies lied in the getting of the code of the modules (in terms of the Dojo - resources) via explicit invocation of the function dojo.require (which is also used to initialise the loaded module). That is in this approach the dependencies were defined directly in the code at those places, where they should to be used.</p><p>Let’s revise our example using Dojo 1.6:</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// file greeting.js</span></span><br><span class="line">dojo.<span class="title function_">provide</span>(<span class="string">&quot;app.greeting&quot;</span>);</span><br><span class="line"></span><br><span class="line">app.<span class="property">greeting</span>.<span class="property">helloInLang</span> = &#123;</span><br><span class="line">  <span class="attr">en</span>: <span class="string">&quot;Hello world!&quot;</span>,</span><br><span class="line">  <span class="attr">es</span>: <span class="string">&quot;¡Hola mundo!&quot;</span>,</span><br><span class="line">  <span class="attr">ru</span>: <span class="string">&quot;Привет мир!&quot;</span>,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">app.<span class="property">greeting</span>.<span class="property">sayHello</span> = <span class="keyword">function</span> (<span class="params">lang</span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> app.<span class="property">greeting</span>.<span class="property">helloInLang</span>[lang];</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// file hello.js</span></span><br><span class="line">dojo.<span class="title function_">provide</span>(<span class="string">&quot;app.hello&quot;</span>);</span><br><span class="line"></span><br><span class="line">dojo.<span class="built_in">require</span>(<span class="string">&quot;app.greeting&quot;</span>);</span><br><span class="line"></span><br><span class="line">app.<span class="property">hello</span> = <span class="keyword">function</span> (<span class="params">x</span>) &#123;</span><br><span class="line">  <span class="variable language_">document</span>.<span class="title function_">write</span>(app.<span class="property">greeting</span>.<span class="title function_">sayHello</span>(<span class="string">&quot;es&quot;</span>));</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure><p>Here we see that modules are defined using the function dojo.provide, and the process of getting of the code of the module starts when you use dojo.require. It is a fairly simple approach that was used in the Dojo up to version 1.7; Google Closure Library uses it to this day.</p><h3 id="The-Namespace-Pattern-2002"><a href="#The-Namespace-Pattern-2002" class="headerlink" title="The Namespace Pattern (2002)"></a>The Namespace Pattern (2002)</h3><blockquote><p>For sloving the issue with name collisions, the namespace pattern was introduced. usually, It is a way to create an Object mounted in Window all method and properties we need reside in it.</p></blockquote><p>Actually Dojo mentioned before in the previous chapter which already use this Parttern.</p><p>If we apply this idea to our example we get something like this:</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// file app.js</span></span><br><span class="line"><span class="keyword">var</span> app = &#123;&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// file greeting.js</span></span><br><span class="line">app.<span class="property">helloInLang</span> = &#123;</span><br><span class="line">  <span class="attr">en</span>: <span class="string">&quot;Hello world!&quot;</span>,</span><br><span class="line">  <span class="attr">es</span>: <span class="string">&quot;¡Hola mundo!&quot;</span>,</span><br><span class="line">  <span class="attr">ru</span>: <span class="string">&quot;Привет мир!&quot;</span>,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// file hello.js</span></span><br><span class="line">app.<span class="property">writeHello</span> = <span class="keyword">function</span> (<span class="params">lang</span>) &#123;</span><br><span class="line">  <span class="variable language_">document</span>.<span class="title function_">write</span>(app.<span class="property">helloInLang</span>[lang]);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure><p>As we can see the logic and the data resides now in the properties of the object app. Thus we don’t pollute the global scope but continue to have access to the various parts of the application from different files.</p><p>But there is a significant flaw that we can modify all the properties of the app object from the global scope.</p><h3 id="The-Module-Pattern-2003"><a href="#The-Module-Pattern-2003" class="headerlink" title="The Module Pattern (2003)"></a>The Module Pattern (2003)</h3><blockquote><p>It’s main idea is encapsulating data and code with a closure and providing access to them through methods accessible from the outside.</p></blockquote><p>Here is a basic example of this type of pattern:</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> greeting = (<span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">  <span class="keyword">var</span> <span class="variable language_">module</span> = &#123;&#125;;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">var</span> helloInLang = &#123;</span><br><span class="line">    <span class="attr">en</span>: <span class="string">&quot;Hello world!&quot;</span>,</span><br><span class="line">    <span class="attr">es</span>: <span class="string">&quot;¡Hola mundo!&quot;</span>,</span><br><span class="line">    <span class="attr">ru</span>: <span class="string">&quot;Привет мир!&quot;</span>,</span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line">  <span class="variable language_">module</span>.<span class="property">getHello</span> = <span class="keyword">function</span> (<span class="params">lang</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> helloInLang[lang];</span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line">  <span class="variable language_">module</span>.<span class="property">writeHello</span> = <span class="keyword">function</span> (<span class="params">lang</span>) &#123;</span><br><span class="line">    <span class="variable language_">document</span>.<span class="title function_">write</span>(<span class="variable language_">module</span>.<span class="title function_">getHello</span>(lang));</span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> <span class="variable language_">module</span>;</span><br><span class="line">&#125;)();</span><br></pre></td></tr></table></figure><p>Here we see the immediately invoked function express, and We can see that the module pattern is very similar to the previous one, but now the data and the methods are encapsulated in a closure.<br>Dive in deep see article <a href="http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html">JavaScript Module Pattern: In-Depth</a></p><h3 id="Template-Defined-Dependencies-2006"><a href="#Template-Defined-Dependencies-2006" class="headerlink" title="Template Defined Dependencies (2006)"></a>Template Defined Dependencies (2006)</h3><blockquote><p>This pattern defines dependencies via inclusion into the target file the special labels.<br>The similars:<br>Comment Defined Dependencies (2006)<br>Externally Defined Dependencies (2007)</p></blockquote><p>The resolving this labels into actual code can be performed via templating, and special build tools, for example, borshik. In contrast to the previous discussed detached dependency definitions patterns, this pattern only works with pre-build step.</p><p>For example:</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// file app.tmp.js</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/*borschik:include:../lib/main.js*/</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/*borschik:include:../lib/helloInLang.js*/</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/*borschik:include:../lib/writeHello.js*/</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// file main.js</span></span><br><span class="line"><span class="keyword">var</span> app = &#123;&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// file helloInLang.js</span></span><br><span class="line">app.<span class="property">helloInLang</span> = &#123;</span><br><span class="line">  <span class="attr">en</span>: <span class="string">&quot;Hello world!&quot;</span>,</span><br><span class="line">  <span class="attr">es</span>: <span class="string">&quot;¡Hola mundo!&quot;</span>,</span><br><span class="line">  <span class="attr">ru</span>: <span class="string">&quot;Привет мир!&quot;</span>,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// file writeHello.js</span></span><br><span class="line">app.<span class="property">writeHello</span> = <span class="keyword">function</span> (<span class="params">lang</span>) &#123;</span><br><span class="line">  <span class="variable language_">document</span>.<span class="title function_">write</span>(app.<span class="property">helloInLang</span>[lang]);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure><h3 id="CommonJs-Modules-2009"><a href="#CommonJs-Modules-2009" class="headerlink" title="CommonJs Modules (2009)"></a>CommonJs Modules (2009)</h3><blockquote><p>CommonJS is the most common module format at the present moment. You can use it not only on the server-side in Node.JS but also on the client-side using Browserfiy or Webpack, which can transform set of CommonJS modules into one bundle.</p></blockquote><p>As an example of the CommonJS module let’s adapt our module by this way:</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// file greeting.js</span></span><br><span class="line"><span class="keyword">var</span> helloInLang = &#123;</span><br><span class="line">  <span class="attr">en</span>: <span class="string">&quot;Hello world!&quot;</span>,</span><br><span class="line">  <span class="attr">es</span>: <span class="string">&quot;¡Hola mundo!&quot;</span>,</span><br><span class="line">  <span class="attr">ru</span>: <span class="string">&quot;Привет мир!&quot;</span>,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> sayHello = <span class="keyword">function</span> (<span class="params">lang</span>) &#123;</span><br><span class="line">  <span class="keyword">return</span> helloInLang[lang];</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">module</span>.<span class="property">exports</span>.<span class="property">sayHello</span> = sayHello;</span><br><span class="line"></span><br><span class="line"><span class="comment">// file hello.js</span></span><br><span class="line"><span class="keyword">var</span> sayHello = <span class="built_in">require</span>(<span class="string">&quot;./lib/greeting&quot;</span>).<span class="property">sayHello</span>;</span><br><span class="line"><span class="keyword">var</span> phrase = <span class="title function_">sayHello</span>(<span class="string">&quot;en&quot;</span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(phrase);</span><br></pre></td></tr></table></figure><h3 id="AMD-2009"><a href="#AMD-2009" class="headerlink" title="AMD (2009)"></a>AMD (2009)</h3><blockquote><p>Base the CommonJs, But loading of the modules should not synchronous, use the browser functionality for the parallel loading of the scripts</p></blockquote><p>If we will rewrite our example using AMD format we will get something like this:</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// file lib/greeting.js</span></span><br><span class="line"><span class="title function_">define</span>(<span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">  <span class="keyword">var</span> helloInLang = &#123;</span><br><span class="line">    <span class="attr">en</span>: <span class="string">&quot;Hello world!&quot;</span>,</span><br><span class="line">    <span class="attr">es</span>: <span class="string">&quot;¡Hola mundo!&quot;</span>,</span><br><span class="line">    <span class="attr">ru</span>: <span class="string">&quot;Привет мир!&quot;</span>,</span><br><span class="line">  &#125;;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">    <span class="attr">sayHello</span>: <span class="keyword">function</span> (<span class="params">lang</span>) &#123;</span><br><span class="line">      <span class="keyword">return</span> helloInLang[lang];</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// file hello.js</span></span><br><span class="line"><span class="title function_">define</span>([<span class="string">&quot;./lib/greeting&quot;</span>], <span class="keyword">function</span> (<span class="params">greeting</span>) &#123;</span><br><span class="line">  <span class="keyword">var</span> phrase = greeting.<span class="title function_">sayHello</span>(<span class="string">&quot;en&quot;</span>);</span><br><span class="line">  <span class="variable language_">document</span>.<span class="title function_">write</span>(phrase);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure><p>The file hello.js is the entry point of the program. In this file there is a function define that declares a module. The first argument of the function is an array of dependencies. The execution of the code of the module, which is defined as a function in the second argument of define, will be launched only after that fact when all dependencies of this module will be loaded. This deferred code execution of the module makes a possibility for the parallel loading of its dependencies.</p><h3 id="UMD-2011"><a href="#UMD-2011" class="headerlink" title="UMD (2011)"></a>UMD (2011)</h3><blockquote><p>There were two formats, CommonJs and AMD, could not get along with each other, so UMD format has been developed for solution of this problem.</p></blockquote><p>As an example let’s refactor our module greeting.js for the simultaneous support of different environments CommonJS and AMD:</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">(<span class="keyword">function</span> (<span class="params">define</span>) &#123;</span><br><span class="line">  <span class="title function_">define</span>(<span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> helloInLang = &#123;</span><br><span class="line">      <span class="attr">en</span>: <span class="string">&quot;Hello world!&quot;</span>,</span><br><span class="line">      <span class="attr">es</span>: <span class="string">&quot;¡Hola mundo!&quot;</span>,</span><br><span class="line">      <span class="attr">ru</span>: <span class="string">&quot;Привет мир!&quot;</span>,</span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      <span class="attr">sayHello</span>: <span class="keyword">function</span> (<span class="params">lang</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> helloInLang[lang];</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;)(</span><br><span class="line">  <span class="keyword">typeof</span> <span class="variable language_">module</span> === <span class="string">&quot;object&quot;</span> &amp;&amp; <span class="variable language_">module</span>.<span class="property">exports</span> &amp;&amp; <span class="keyword">typeof</span> define !== <span class="string">&quot;function&quot;</span></span><br><span class="line">    ? <span class="keyword">function</span> (<span class="params">factory</span>) &#123;</span><br><span class="line">        <span class="variable language_">module</span>.<span class="property">exports</span> = <span class="title function_">factory</span>();</span><br><span class="line">      &#125;</span><br><span class="line">    : define</span><br><span class="line">);</span><br></pre></td></tr></table></figure><p>In the heart of this implementation pattern lies the immediately invoked function expression. That function takes different arguments depending on the environment. The passed argument is the following function if the code is used as a CommonJS module:</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> (<span class="params">factory</span>) &#123;</span><br><span class="line">    <span class="variable language_">module</span>.<span class="property">exports</span> = <span class="title function_">factory</span>();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>If the code is used as an AMD module, the argument of function is define. Due this substitution the code can be used in different environments.</p><h3 id="ES2015-Modules-2015"><a href="#ES2015-Modules-2015" class="headerlink" title="ES2015 Modules (2015)"></a>ES2015 Modules (2015)</h3><p>By tradition, let’s adapt our example to show the specification in action:</p><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// file lib/greeting.js</span></span><br><span class="line"><span class="keyword">const</span> helloInLang = &#123;</span><br><span class="line">  <span class="attr">en</span>: <span class="string">&quot;Hello world!&quot;</span>,</span><br><span class="line">  <span class="attr">es</span>: <span class="string">&quot;¡Hola mundo!&quot;</span>,</span><br><span class="line">  <span class="attr">ru</span>: <span class="string">&quot;Привет мир!&quot;</span>,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> greeting = &#123;</span><br><span class="line">  <span class="attr">sayHello</span>: <span class="keyword">function</span> (<span class="params">lang</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> helloInLang[lang];</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// file hello.js</span></span><br><span class="line"><span class="keyword">import</span> &#123; greeting &#125; <span class="keyword">from</span> <span class="string">&quot;./lib/greeting&quot;</span>;</span><br><span class="line"><span class="keyword">const</span> phrase = greeting.<span class="title function_">sayHello</span>(<span class="string">&quot;en&quot;</span>);</span><br><span class="line"><span class="variable language_">document</span>.<span class="title function_">write</span>(phrase);</span><br></pre></td></tr></table></figure>]]>
    </content>
    <id>https://liuduwei-blog.pages.dev/2022/05/16/History-of-javascript-module-development/</id>
    <link href="https://liuduwei-blog.pages.dev/2022/05/16/History-of-javascript-module-development/"/>
    <published>2022-05-16T10:07:48.000Z</published>
    <summary>
      <![CDATA[<blockquote>
<p>Recently I came across an article call <a href="https://github.com/myshov/history-of-javascript/tree/master/4_evolution_of_j]]>
    </summary>
    <title>History of javascript module development</title>
    <updated>2026-12-01T15:40:31.640Z</updated>
  </entry>
  <entry>
    <author>
      <name>Leo Liu</name>
    </author>
    <category term="Javascript" scheme="https://liuduwei-blog.pages.dev/tags/Javascript/"/>
    <content>
      <![CDATA[<table><thead><tr><th>Aspect</th><th>var</th><th>let</th><th>const</th></tr></thead><tbody><tr><td>Scope</td><td>Function scope; variable is accessible inside function even if declared after usage</td><td>Block scope; variable is only accessible inside nearest enclosing {…}</td><td>Block scope; variable is only accessible inside nearest enclosing {…}</td></tr><tr><td>Re-declared</td><td>Can re-declare var anywhere in the function</td><td>Cannot re-declare let in the same block</td><td>Cannot re-declare const anywhere</td></tr><tr><td>Updated</td><td>Can update&#x2F;reassign the value</td><td>Can update&#x2F;reassign the value</td><td>Cannot update&#x2F;reassign the value, must be initialized and value cannot change</td></tr><tr><td>Hoisted</td><td>Function scoped variables are hoisted to the top of the function</td><td>hoisted to the top of its block or scope, but it is not initialized. Accessing the variable before its declaration results in a ReferenceError.</td><td>hoisted to the top of its block or scope, but it is not initialized. Accessing the variable before its declaration results in a ReferenceError.</td></tr></tbody></table>]]>
    </content>
    <id>https://liuduwei-blog.pages.dev/2022/04/16/Var-let-const/</id>
    <link href="https://liuduwei-blog.pages.dev/2022/04/16/Var-let-const/"/>
    <published>2022-04-16T08:35:29.000Z</published>
    <summary>
      <![CDATA[<table>
<thead>
<tr>
<th>Aspect</th>
<th>var</th>
<th>let</th>
<th>const</th>
</tr>
</thead>
<tbody><tr>
<td>Scope</td>
<td>Function scope;]]>
    </summary>
    <title>The Var, let, Const - What's the Difference?</title>
    <updated>2026-12-01T15:40:31.640Z</updated>
  </entry>
  <entry>
    <author>
      <name>Leo Liu</name>
    </author>
    <category term="Javascript" scheme="https://liuduwei-blog.pages.dev/tags/Javascript/"/>
    <category term="Front-end" scheme="https://liuduwei-blog.pages.dev/tags/Front-end/"/>
    <category term="Html-event" scheme="https://liuduwei-blog.pages.dev/tags/Html-event/"/>
    <content>
      <![CDATA[<blockquote><p>总结一下使用三种不同的方式加载JS脚本，对DomContentLoaded事件的影响</p></blockquote><p>在本文中，我们将总结一下使用三种不同的方式加载JS脚本，对DomContentLoaded事件的影响。DomContentLoaded事件是指浏览器解析完HTML文档，构建了DOM树后触发的事件，它通常用于执行一些依赖于DOM元素的操作。加载JS脚本有三种方式：默认加载、defer和async，它们分别有不同的特点和影响。</p><h2 id="默认加载"><a href="#默认加载" class="headerlink" title="默认加载"></a>默认加载</h2><blockquote><p>默认加载是最常见的一种方式，就是直接在HTML文档中使用<script src="test.js"></script>标签引入外部脚本。这种方式的特点是：</p></blockquote><ul><li>脚本会按照声明的顺序依次执行。</li><li>脚本会阻塞HTML文档的解析和DOM树的构建，直到脚本执行完毕。</li><li>脚本会阻塞后面的脚本和样式表的加载和渲染。</li><li>脚本会延迟DomContentLoaded事件的触发，直到所有的脚本执行完毕。</li></ul><p>这种方式的优点是简单易用，保证了脚本之间的依赖关系。缺点是会影响页面的性能和用户体验，因为页面会出现白屏或者卡顿的现象。</p><h2 id="defer"><a href="#defer" class="headerlink" title="defer"></a>defer</h2><blockquote><p>defer是一个HTML5新增的属性，可以用于<script>标签，表示延迟执行脚本。这种方式的特点是：</p></blockquote><ul><li>脚本会按照声明的顺序依次执行。</li><li>脚本不会阻塞HTML文档的解析和DOM树的构建，而是在后台异步下载。</li><li>脚本会在DomContentLoaded事件之前执行，但不会阻塞该事件的触发。</li><li>脚本不会阻塞后面的脚本和样式表的加载和渲染。</li></ul><p>这种方式的优点是可以提高页面的性能和用户体验，因为页面不会出现白屏或者卡顿的现象。缺点是不支持老旧的浏览器。</p><h2 id="async"><a href="#async" class="headerlink" title="async"></a>async</h2><blockquote><p>async也是一个HTML5新增的属性，可以用于<script>标签，表示异步执行脚本。这种方式的特点是：</p></blockquote><ul><li>脚本不会按照声明的顺序执行，而是哪个脚本先下载完就先执行。</li><li>脚本不会阻塞HTML文档的解析和DOM树的构建，而是在后台异步下载。</li><li>脚本不会等待DomContentLoaded事件，而是一旦下载完就立即执行，并可能阻塞该事件的触发。</li><li>脚本不会阻塞后面的脚本和样式表的加载和渲染。</li></ul><p>这种方式的优点是可以最大程度地利用网络资源，提高页面加载速度。缺点是无法保证脚本之间的依赖关系，可能导致错误或者意外结果。</p><h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>使用三种不同的方式加载JS脚本，对DomContentLoaded事件有不同程度的影响。默认加载会延迟该事件的触发，defer会在该事件之前执行，async则可能阻塞该事件的触发。根据不同场景和需求，我们可以选择合适的方式来优化页面性能和用户体验。</p>]]>
    </content>
    <id>https://liuduwei-blog.pages.dev/2022/03/31/script-load-ways/</id>
    <link href="https://liuduwei-blog.pages.dev/2022/03/31/script-load-ways/"/>
    <published>2022-03-31T12:17:31.000Z</published>
    <summary>
      <![CDATA[<blockquote>
<p>总结一下使用三种不同的方式加载JS脚本，对DomContentLoaded事件的影响</p>
</blockquote>
<p>在本文中，我们将总结一下使用三种不同的方式加载JS脚本，对DomContentLoaded事件的影响。DomConten]]>
    </summary>
    <title>加载JS脚本的三种方法</title>
    <updated>2026-12-01T15:40:31.640Z</updated>
  </entry>
  <entry>
    <author>
      <name>Leo Liu</name>
    </author>
    <category term="Javascript" scheme="https://liuduwei-blog.pages.dev/tags/Javascript/"/>
    <category term="Algorithm" scheme="https://liuduwei-blog.pages.dev/tags/Algorithm/"/>
    <category term="Sort" scheme="https://liuduwei-blog.pages.dev/tags/Sort/"/>
    <content>
      <![CDATA[<p>Shell Sort is a very efficient sorting algorithm; its time complexity can almost reach O(n) in real-world production.</p><p>The code Shell Sort moves elements in the same way as Selection Sort, but it seeks local sorting and then achieves global sorting.</p><p>Let’s see How it works:</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">var &#123; less &#125; = require(&quot;./Shuffle&quot;);</span><br><span class="line"></span><br><span class="line">function shellSort(a) &#123;</span><br><span class="line">  var length = a.length;</span><br><span class="line">  var h = 1;</span><br><span class="line">  while (h &lt; Math.floor(length / 3)) h = 3 * h + 1; //3x + 1 increament</span><br><span class="line"></span><br><span class="line">  while (h &gt;= 1) &#123;</span><br><span class="line">    for (let i = h; i &lt; length; i++) &#123;</span><br><span class="line">      // h-insertion_sort</span><br><span class="line">      for (let j = i; j &gt;= h &amp;&amp; less(a[j], a[j - h]); j -= h) &#123;</span><br><span class="line">        let temp = a[j - h];</span><br><span class="line">        a[j - h] = a[j];</span><br><span class="line">        a[j] = temp;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    h = Math.floor(h / 3); // next increament</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">module.exports = shellSort;</span><br></pre></td></tr></table></figure><p>Why is Shell Sort so effective compared to Selection Sort? It<br>moves elements in multiple positions, the number of increments every time, but Selection Sort moves elements in only one position.</p><p>Shell Sort is simpler than Quick Sort, so the application is widespread in small embedded devices.</p>]]>
    </content>
    <id>https://liuduwei-blog.pages.dev/2022/03/23/Sort-Algorithm%EF%BC%882%EF%BC%89%EF%BC%9A-Elementary-Sort-Javascript-Implement-Shell-Sort/</id>
    <link href="https://liuduwei-blog.pages.dev/2022/03/23/Sort-Algorithm%EF%BC%882%EF%BC%89%EF%BC%9A-Elementary-Sort-Javascript-Implement-Shell-Sort/"/>
    <published>2022-03-23T12:30:41.000Z</published>
    <summary>
      <![CDATA[<p>Shell Sort is a very efficient sorting algorithm; its time complexity can almost reach O(n) in real-world production.</p>
<p>The code She]]>
    </summary>
    <title>Sort Algorithm（2）： Elementary Sort Javascript Implement - Shell Sort</title>
    <updated>2026-12-01T15:40:31.640Z</updated>
  </entry>
  <entry>
    <author>
      <name>Leo Liu</name>
    </author>
    <category term="Anki" scheme="https://liuduwei-blog.pages.dev/tags/Anki/"/>
    <content>
      <![CDATA[<blockquote><p>自使用 Anki 以来，如何录入每日生活中遇到的生词一直困扰着我，直到遇到了这款插件 Dic2Anki</p></blockquote><h2 id="插件简介"><a href="#插件简介" class="headerlink" title="插件简介"></a>插件简介</h2><p>Dic2Anki 支持同步 Eudic 和有道词典的生词本到 Anki，使用自带模板（比较丑）,同步时自动查询，查询能选择插件自带的 Eudic 或者是有道的 api, 帐号能通过帐号密码二维码或者是 cookies 登录，还是比较方便的</p><h2 id="插件安装"><a href="#插件安装" class="headerlink" title="插件安装"></a>插件安装</h2><p>Add-ons 代码：1284759083<br>(Ankiweb 页面)[<a href="https://ankiweb.net/shared/info/1284759083]">https://ankiweb.net/shared/info/1284759083]</a> :</p><h2 id="模板改进"><a href="#模板改进" class="headerlink" title="模板改进"></a>模板改进</h2><p>这个自带的模板查询出来是真的丑的不能看，建议改一下模板类型，只保留单词字段，配合 fastwq 来查询</p>]]>
    </content>
    <id>https://liuduwei-blog.pages.dev/2021/04/04/Anki-2-%E6%8F%92%E4%BB%B6-Dic2Anki/</id>
    <link href="https://liuduwei-blog.pages.dev/2021/04/04/Anki-2-%E6%8F%92%E4%BB%B6-Dic2Anki/"/>
    <published>2021-04-04T03:52:32.000Z</published>
    <summary>
      <![CDATA[<blockquote>
<p>自使用 Anki 以来，如何录入每日生活中遇到的生词一直困扰着我，直到遇到了这款插件 Dic2Anki</p>
</blockquote>
<h2 id="插件简介"><a href="#插件简介" class="headerlink" title]]>
    </summary>
    <title>Anki插件介绍-Dic2Anki</title>
    <updated>2026-12-01T15:40:31.640Z</updated>
  </entry>
  <entry>
    <author>
      <name>Leo Liu</name>
    </author>
    <category term="Anki" scheme="https://liuduwei-blog.pages.dev/tags/Anki/"/>
    <category term="Study" scheme="https://liuduwei-blog.pages.dev/tags/Study/"/>
    <content>
      <![CDATA[<blockquote><p>用 Anki 背单词好一段时间了,期间一直在探索如何优雅的批量制出质量不错的卡片，到现在为止，算是掌握了两种方法</p></blockquote><h2 id="方法一：通过开源项目-FastWq-制卡"><a href="#方法一：通过开源项目-FastWq-制卡" class="headerlink" title="方法一：通过开源项目 FastWq 制卡"></a>方法一：通过开源项目 FastWq 制卡</h2><blockquote><p>项目地址<a href="https://github.com/sth2018/FastWordQuery">https://github.com/sth2018/FastWordQuery</a></p></blockquote><h4 id="效果如下图："><a href="#效果如下图：" class="headerlink" title="效果如下图："></a>效果如下图：</h4><p><img src="/../images/fastwq1.png" alt="fastwq1"><br><img src="/../images/fastwq2.png" alt="fastwq2"></p><h4 id="制卡方法"><a href="#制卡方法" class="headerlink" title="制卡方法:"></a>制卡方法:</h4><ol><li><p><strong>安装插件</strong></p><p>请根据<a href="https://github.com/sth2018/FastWordQuery">项目</a>ReadMe 文件安装，这里不再赘述.</p></li><li><p><strong>选择模板</strong></p><p>使用插件查询需要先选定模板，模板选取自己喜欢的就好了，这里有两个个开源项目(<a href="https://github.com/ninja33/anki-templates">1</a>,<a href="https://github.com/Troyciv/anki-templates-superlist">2</a>)收集了一些优秀模板，可以参考一下，注意的是有些模板的配色和查询结果的配色或者暗黑模式不兼容，需要自行修改一下模板的 html 或者 css 文件使得卡片字段一目了然</p></li><li><p><strong>插件配置</strong></p><p>详细的使用教程 ReadMe 讲的很清楚了,但是这里再上补充一下：插件的有些选项是无效的，或者选项返回的结果很离谱.</p><p>经过我的简单摸索，我自己的模板使用这个配置制作的卡片效果比较好:<br><img src="/../images/fastwqconfig.png" alt="fastwqcg"></p></li></ol><h2 id="方法二：通过-Excel-制作卡片"><a href="#方法二：通过-Excel-制作卡片" class="headerlink" title="方法二：通过 Excel 制作卡片"></a>方法二：通过 Excel 制作卡片</h2><blockquote><p>此方法是在知呼上看到的，点击链接跳转</p></blockquote><p>需要补充的是，Excel 制卡文件只能在 windows 下用微软 Execl 打开，不支持 linux 下的 wps(win10 下的 wps 没测试过)</p><h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><ol><li><p>Excel 制卡的方法很简单，基本开箱即用，一点都不需要额外的配置，但是缺点是比较麻烦，效率低</p></li><li><p>FastWq 配置起来比较麻烦，但是配置好过后效果非常好，支持本地字典查询模板字段自定义, 不需要任何第三方工具，方便且高效（也许 Execl 的法二改一改源代码也可以)</p></li></ol><p><strong>希望之后还能找到更方便高效的方法，有时间甚至我都想自己写一个插件了，当然这都是后话了，我的当务之急是用好 Anki 这个神器考研冲冲冲</strong></p>]]>
    </content>
    <id>https://liuduwei-blog.pages.dev/2021/02/28/Anki-1-%E5%8D%95%E8%AF%8D%E5%8D%A1%E6%89%B9%E9%87%8F%E5%88%B6%E4%BD%9C%E7%9A%84%E6%96%B9%E6%B3%95/</id>
    <link href="https://liuduwei-blog.pages.dev/2021/02/28/Anki-1-%E5%8D%95%E8%AF%8D%E5%8D%A1%E6%89%B9%E9%87%8F%E5%88%B6%E4%BD%9C%E7%9A%84%E6%96%B9%E6%B3%95/"/>
    <published>2021-02-28T10:19:00.000Z</published>
    <summary>
      <![CDATA[<blockquote>
<p>用 Anki 背单词好一段时间了,期间一直在探索如何优雅的批量制出质量不错的卡片，到现在为止，算是掌握了两种方法</p>
</blockquote>
<h2 id="方法一：通过开源项目-FastWq-制卡"><a href="#方法一：通过开源项]]>
    </summary>
    <title>批量制作单词卡的方法</title>
    <updated>2026-12-01T15:40:31.640Z</updated>
  </entry>
  <entry>
    <author>
      <name>Leo Liu</name>
    </author>
    <category term="Hexo" scheme="https://liuduwei-blog.pages.dev/tags/Hexo/"/>
    <content>
      <![CDATA[<blockquote><p>GithubPages 在国内的访问速度实在太慢了，看了下 cdn 加速，要备案什么的太麻烦，于是找到了 Netlify 这个还算不错的解决方案（主要是免费 😏）</p></blockquote><h2 id="注册Netlify"><a href="#注册Netlify" class="headerlink" title="注册Netlify"></a>注册<a href="https://www.netlify.com/">Netlify</a></h2><p>第一个选项就是 Github 登录, 一路确认就好了</p><h2 id="绑定仓库"><a href="#绑定仓库" class="headerlink" title="绑定仓库"></a>绑定仓库</h2><p>Github 认证登录过后，右边应该有个 Site from git. 点击进去一路确认，选择你的 blog 源代码仓库. 没记错之后会自动跳到部署页面</p><h2 id="部署"><a href="#部署" class="headerlink" title="部署"></a>部署</h2><blockquote><p>这一步稍微复杂一点点</p></blockquote><ol><li>部署页面最下段会出现一个 build command 选项:<br><img src="/../images/Netlify1.png" alt="deploy-site"></li></ol><center style="font-size:14px;color:#C0C0C0;text-decoration:underline">注意截图和第一次部署不一样</center><ol start="2"><li></li></ol><ul><li><p>Hexo 选项如下:<br><img src="/../images/build.png" alt="Build"></p></li><li><p>注意这一项一定要填写 Hexo 源码所在的 branch:<br><img src="/../images/site.png" alt="Site"></p></li></ul><ol start="3"><li>如果是静态网站甚至可以不用加 bulid 命令，另外还支持其他很多 Blog 框架的部署(请自行<a href="https://docs.netlify.com/configure-builds/common-configurations/">阅读文档</a>)</li></ol><h2 id="自定义二级域名"><a href="#自定义二级域名" class="headerlink" title="自定义二级域名"></a>自定义二级域名</h2><p>部署成功后会自动生成一个二级域名,但是前缀可以修改:<br><img src="/../images/set.png" alt="set"></p><h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>测试了一下，不挂代理确实快很多，但是第一次图片还是加载的特别慢,但是比 github 随时 connect refused 好多了.</p><p>挂代理 github 快一些.</p>]]>
    </content>
    <id>https://liuduwei-blog.pages.dev/2021/02/10/deploy-on-netlify/</id>
    <link href="https://liuduwei-blog.pages.dev/2021/02/10/deploy-on-netlify/"/>
    <published>2021-02-10T13:07:55.000Z</published>
    <summary>
      <![CDATA[<blockquote>
<p>GithubPages 在国内的访问速度实在太慢了，看了下 cdn 加速，要备案什么的太麻烦，于是找到了 Netlify 这个还算不错的解决方案（主要是免费 😏）</p>
</blockquote>
<h2 id="注册Netlify"><a h]]>
    </summary>
    <title>部署博客到Netlify（智能CDN加速）</title>
    <updated>2026-12-01T15:40:31.640Z</updated>
  </entry>
  <entry>
    <author>
      <name>Leo Liu</name>
    </author>
    <category term="Hexo" scheme="https://liuduwei-blog.pages.dev/tags/Hexo/"/>
    <content>
      <![CDATA[<ol><li><p>修改 config.yml<br>post_asset-folder: ture</p></li><li><p>安装插件<br>cnpm install hexo-filter-image –save</p></li></ol>]]>
    </content>
    <id>https://liuduwei-blog.pages.dev/2021/02/08/Hexo-fix-imagectload/</id>
    <link href="https://liuduwei-blog.pages.dev/2021/02/08/Hexo-fix-imagectload/"/>
    <published>2021-02-08T07:29:08.000Z</published>
    <summary>
      <![CDATA[<ol>
<li><p>修改 config.yml<br>post_asset-folder: ture</p>
</li>
<li><p>安装插件<br>cnpm install hexo-filter-image –save</p>
</li>
</ol>]]>
    </summary>
    <title>Hexo修复文章图片无法加载</title>
    <updated>2026-12-01T15:40:31.640Z</updated>
  </entry>
  <entry>
    <author>
      <name>Leo Liu</name>
    </author>
    <category term="Archlinux" scheme="https://liuduwei-blog.pages.dev/tags/Archlinux/"/>
    <content>
      <![CDATA[<h1 id="效果如下"><a href="#效果如下" class="headerlink" title="效果如下:"></a>效果如下:</h1><p><img src="/../images/overlook.png" alt="overlook"><br><img src="/../images/neofetch.png" alt="neofetch"><br><img src="/../images/nemo.png" alt="directory"><br><img src="/../images/tweak.png" alt="tweak"></p><h1 id="安装"><a href="#安装" class="headerlink" title="安装:"></a>安装:</h1><blockquote><p>所有主题均在<a href="https://www.pling.com/s/Gnome/browse">GNOME-LOOK.ORG</a>安装</p></blockquote><ol><li>sudo pacman -S ocs-url</li><li>点击主题右窗口install</li></ol>]]>
    </content>
    <id>https://liuduwei-blog.pages.dev/2021/02/08/Archlinux-gnome-optimus/</id>
    <link href="https://liuduwei-blog.pages.dev/2021/02/08/Archlinux-gnome-optimus/"/>
    <published>2021-02-08T06:49:50.000Z</published>
    <summary>
      <![CDATA[<h1 id="效果如下"><a href="#效果如下" class="headerlink" title="效果如下:"></a>效果如下:</h1><p><img src="/../images/overlook.png" alt="overlook"><br><img s]]>
    </summary>
    <title>Archlinux-Gnome主题优化</title>
    <updated>2026-12-01T15:40:31.640Z</updated>
  </entry>
  <entry>
    <author>
      <name>Leo Liu</name>
    </author>
    <category term="Archlinux" scheme="https://liuduwei-blog.pages.dev/tags/Archlinux/"/>
    <content>
      <![CDATA[<h1 id="location"><a href="#location" class="headerlink" title="location"></a>location</h1><p>&#x2F;var&#x2F;log&#x2F;</p><h1 id="journalctl"><a href="#journalctl" class="headerlink" title="journalctl"></a>journalctl</h1><ol><li>journalctl -u gdm</li><li>journalctl -b</li></ol>]]>
    </content>
    <id>https://liuduwei-blog.pages.dev/2021/02/08/Archlinux-logslocaltion/</id>
    <link href="https://liuduwei-blog.pages.dev/2021/02/08/Archlinux-logslocaltion/"/>
    <published>2021-02-08T03:36:33.000Z</published>
    <summary>
      <![CDATA[<h1 id="location"><a href="#location" class="headerlink" title="location"></a>location</h1><p>&#x2F;var&#x2F;log&#x2F;</p>
<h1 id="journalct]]>
    </summary>
    <title>Archlinux-如何查看日志</title>
    <updated>2026-12-01T15:40:31.640Z</updated>
  </entry>
  <entry>
    <author>
      <name>Leo Liu</name>
    </author>
    <category term="Archlinux" scheme="https://liuduwei-blog.pages.dev/tags/Archlinux/"/>
    <category term="Gnome" scheme="https://liuduwei-blog.pages.dev/tags/Gnome/"/>
    <content>
      <![CDATA[<ol><li><a href="https://www.gauthampdas.com/blog/tech/linux/resetting-default-file-manager-in-gnome">https://www.gauthampdas.com/blog/tech/linux/resetting-default-file-manager-in-gnome</a></li></ol>]]>
    </content>
    <id>https://liuduwei-blog.pages.dev/2021/02/07/Archlinux-change2nemo/</id>
    <link href="https://liuduwei-blog.pages.dev/2021/02/07/Archlinux-change2nemo/"/>
    <published>2021-02-07T12:58:44.000Z</published>
    <summary>
      <![CDATA[<ol>
<li><a href="https://www.gauthampdas.com/blog/tech/linux/resetting-default-file-manager-in-gnome">https://www.gauthampdas.com/blog/tech]]>
    </summary>
    <title>更换Gnome默认文件浏览器</title>
    <updated>2026-12-01T15:40:31.640Z</updated>
  </entry>
  <entry>
    <author>
      <name>Leo Liu</name>
    </author>
    <category term="Archlinux" scheme="https://liuduwei-blog.pages.dev/tags/Archlinux/"/>
    <content>
      <![CDATA[<h1 id="安全卸载软件包及其依赖"><a href="#安全卸载软件包及其依赖" class="headerlink" title="安全卸载软件包及其依赖"></a>安全卸载软件包及其依赖</h1><p>sudo pacman -Rs<br>sudo pacman -R $(pacman -Qdtq)<br>sudo pacamn -Rsu</p>]]>
    </content>
    <id>https://liuduwei-blog.pages.dev/2021/02/07/Archlinux-pacman/</id>
    <link href="https://liuduwei-blog.pages.dev/2021/02/07/Archlinux-pacman/"/>
    <published>2021-02-07T08:05:17.000Z</published>
    <summary>
      <![CDATA[<h1 id="安全卸载软件包及其依赖"><a href="#安全卸载软件包及其依赖" class="headerlink" title="安全卸载软件包及其依赖"></a>安全卸载软件包及其依赖</h1><p>sudo pacman -Rs<br>sudo pacman -R]]>
    </summary>
    <title>Archlinux-如何安全卸载软件</title>
    <updated>2026-12-01T15:40:31.640Z</updated>
  </entry>
</feed>
