-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreactivity-vuemastery.js
53 lines (47 loc) · 1.12 KB
/
reactivity-vuemastery.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
let data={price:0,quantity:0}
let target,total,doubleTotal
class Dep{
constructor(){
this.subscribers = []
}
depend(){
if(target && ! this.subscribers.includes(target)){
this.subscribers.push(target)
}
}
notify(){
this.subscribers.forEach(sub => {
sub()
})
}
}
Object.keys(data).forEach(key=>{
let value=data[key]
const dep=new Dep()
Object.defineProperty(data,key,{
get(){
dep.depend();
return value
},
set(newVal){
value=newVal
dep.notify();
}
})
})
function watcher(myFunc){
target=myFunc;
target();
target=null
}
watcher(()=>{
total=data.price*data.quantity
})
watcher(()=>{
doubleTotal=data.price*data.quantity*2
})
console.log("**********************************");
console.log("You Have This Object : ",data)
console.log(`You Can Set Or Get Price|Quantity - currnet Price = ${data.price} and current Quantity = ${data.quantity}`)
console.log(`you can get total and doubleTotal`);
console.log("**********************************");