-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path观察者模式.js
54 lines (48 loc) · 1.15 KB
/
观察者模式.js
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
// proxy实现
const queue = new Set()
const observe = fn=>{queue.add(fn)}
const observable = obj=>new Proxy(obj,{set,get})
var people = observable({name:'liyu',age:24})
observe(printName)
observe(printAge)
function set(target,key,value,receiver){
queue.forEach(obs=>obs.call(target))
return Reflect.set(target,key,value,receiver)
}
function get(target,key,receiver){
console.log(target[key])
return Reflect.get(target,key,receiver)
}
function printName(){
console.log(this.name)
}
function printAge(){
console.log(this.age)
}
// defineProperty实现
function observe(obj,key){
let old = obj[key]
// 此处应该是一个闭包,因为Object.defineProperty是可以全局触发的
// 而old在函数内调用,所以old并不会被立即释放,而是被缓存
Object.defineProperty(obj,key,{
enumerable:true,
configurable:true,
set:function(newValue){
if(old != newValue){
console.log('set:',newValue)
}
old = newValue
},
get:function(){
console.log('get:',old)
return old
}
})
//闭包
return function(){
return old;
}
}
var people = {name:'liyu',age:23}
var _name = observe(people,'name')
var _age = observe(people,'age')