forked from Code-Pop/vue-3-reactivity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
06-reactive.js
25 lines (22 loc) · 915 Bytes
/
06-reactive.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
// Notice our get/set has an additional parameter called receiver which we’re
// sending as an argument into `Reflect.get/set`. This ensures that the proper value
// of `this` is used when our object has inherited values / functions from
// another object. This is why we always use `Reflect` inside of a `Proxy`, so
// we can keep the original behavior we are customizing.
function reactive(target) {
const handler = {
get(target, key, receiver) {
console.log(`Get was called with key: ${key}`)
return Reflect.get(target, key, receiver)
},
set(target, key, value, receiver) {
console.log(`Set was called with key: ${key}, value: ${value}`)
return Reflect.set(target, key, value, receiver)
},
}
return new Proxy(target, handler)
}
const product = reactive({ price: 5, quantity: 2 })
console.log(product.quantity)
product.quantity = 4
console.log(product.quantity)