Skip to content

proxied

Create a dynamic proxied a object

75 bytes

Usage

Javascript’s Proxy object is powerful but a bit awkward to use. The _.proxied function creates the Proxy for you and handles calling back to your handler when functions on the Proxy are called or properties are accessed.

import * as _ from 'radashi'
type Property = 'name' | 'size' | 'getLocation'
const person = _.proxied((prop: Property) => {
switch (prop) {
case 'name':
return 'Joe'
case 'size':
return 20
case 'getLocation'
return () => 'here'
}
})
person.name // => Joe
person.size // => 20
person.getLocation() // => here