Last active
February 1, 2018 07:08
-
-
Save AaronFlower/a366d2fa602d4ce738b7c5d99ad8326a to your computer and use it in GitHub Desktop.
A observable function use Proxy and Reflect
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function observable (obj, onchange) { | |
return new Proxy(obj, { | |
set (target, key, value) { | |
Reflect.set(target, key, value) | |
onchange(key, value) | |
}, | |
delete (target, key, value) { | |
Reflect.delete(key) | |
onchange(key, undefined) | |
} | |
}) | |
} | |
let a = {} | |
let pA = observable(a, (k, v) => console.log('Observalbe:', k, v)) | |
pA.foo = 'bar' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment