惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
MyScale Blog
MyScale Blog
博客园 - Franky
The Cloudflare Blog
IT之家
IT之家
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
WordPress大学
WordPress大学
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
The Blog of Author Tim Ferriss
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
Google DeepMind News
Google DeepMind News
P
Proofpoint News Feed
Martin Fowler
Martin Fowler
aimingoo的专栏
aimingoo的专栏
J
Java Code Geeks
腾讯CDC
雷峰网
雷峰网
Microsoft Azure Blog
Microsoft Azure Blog
G
Google Developers Blog
博客园 - 【当耐特】
美团技术团队
云风的 BLOG
云风的 BLOG

BlackHoleMax

用 Podman 在家用电脑搭私人 WebDAV 书签同步服务 中国校服外包产业与“统一着装”的意识形态机制 赛里斯教育臃肿、企业文化与社会不平等的结构性困境 C语言学习(帕斯卡三角) C语言学习(long和char强制类型转换) 意识形态国家机器(AIE)笔记 论阶级革命 论亲情 论盲目 当代唯分论,“什么书该读” 农场主与笨驴 如何成为一名安那其主义者 windows平台最好的包管理器:scoop 从Python3快速开始 正在施工🚧
Vue 响应式原理:从 2.x 的 Object.defineProperty 到 3.x 的...
BlackHoleMax · 2025-09-25 · via BlackHoleMax
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const targetMap = new WeakMap();

function reactive(obj) {
return new Proxy(obj, {
get(target, key, receiver) {
const res = Reflect.get(target, key, receiver);
track(target, key);
return isObject(res) ? reactive(res) : res;
},
set(target, key, val, receiver) {
const oldVal = target[key];
const hadKey = hasOwn(target, key);
const result = Reflect.set(target, key, val, receiver);
if (!hadKey) trigger(target, key, 'add');
else if (oldVal !== val) trigger(target, key, 'set');
return result;
},
deleteProperty(target, key) {
const hadKey = hasOwn(target, key);
const result = Reflect.deleteProperty(target, key);
if (hadKey) trigger(target, key, 'delete');
return result;
}
});
}
1
2
3
4
5
6
7
8
9
10
11
12
13
Vue2 响应式
├─ 入口:new Vue() → initState() → initData()
├─ 遍历 data → observe(obj) → new Observer()
├─ 为每个属性 defineReactive → getter/setter
├─ Dep 管理依赖,Watcher 订阅
└─ 缺陷:新增属性、数组索引、深层递归

Vue3 响应式
├─ 入口:createApp() → setup() → reactive()
├─ 一次性 new Proxy() 代理根对象
├─ track() 收集,trigger() 派发
├─ WeakMap 全局缓存,懒代理
└─ 解决 Vue2 所有盲区,性能更好

“Vue2 通过 Object.defineProperty 递归属性实现数据劫持,新增/删除属性无法自动响应;
Vue3 改用 Proxy 整体代理对象,能拦截 13 种操作,新增/删除属性、数组索引均可自动追踪,且采用懒代理策略,启动更快。”