Skip to content

Instantly share code, notes, and snippets.

@Veejay Veejay/proxy.js
Last active Nov 7, 2017

Embed
What would you like to do?
Object extension
const wrap = (object) => {
return new Proxy(object, {
get(target, property, receiver) {
switch (property) {
case 'store':
return (...args) => {
return Reflect.set(object, ...args)
}
break
case 'fetch':
return (...args) => {
const [key, defaultValue] = args
return Reflect.get(object, key) || defaultValue
}
break
case 'map':
return (...args) => {
const [mapper] = args
return Object.keys(object).map(key => {
return mapper(key, object[key])
})
}
}
}
})
}
```
const a = {foobar: 2}
const b = wrap(a)
b.store('lukaku', 9)
=> true
b.fetch('foobar')
=> 2
a
=> {foobar: 2, lukaku: 9}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.