forked from amebyte/mini-vue2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vue2.html
57 lines (54 loc) · 1.15 KB
/
vue2.html
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Reactive</title>
</head>
<body>
<div id="app">初始值</div>
<script>
// vue.util.defineReactive(obj, key, val)
function defineReactive(obj, key, val) {
observe(val)
Object.defineProperty(obj, key, {
get() {
console.log('get', key, val)
return val
},
set(v) {
console.log('set', v)
observe(v)
val = v
update()
}
})
}
function observe(val) {
if(typeof val !== 'object' || val === null || val === undefined){
return
}
Object.keys(val).forEach(key => {
defineReactive(val, key, val[key])
})
}
function set(obj, key, val){
defineReactive(obj, key, val)
}
function update() {
app.innerHTML = o.a
}
let o = {a:'2', b:{c: 3}}
// defineReactive(o, 'a', 1)
observe(o)
o.a
o.a = 2
set(o, 'd', 'd')
o.d
// setInterval(() => {
// o.b.c = new Date().toLocaleTimeString()
// }, 1000)
</script>
</body>
</html>