A simple reusable proxy to allow custom subscript access
npm install @nejs/subscript-proxyA powerful and flexible JavaScript library for allowing easy and programmatic subscript access to any object.
- Allows programmatic methods for defining how an object should react when non-existing keys are
accessed.
- Allow set traps instead of only get traps
- Allow option to simply return proxy around original object rather than modifying prototype chain
In a node js project, simply perform the following npm
install command.
``bash`
npm install @nejs/subscript-proxy
In a browser, a script tag pointed here can get you a CDN
served version of the latest build
`html`
Or if you want it to automatically be injected into the window,
you can use the iffy variant.
`html`
`js
import { SubscriptProxy } from '@nejs/subscript-proxy';
// or alternatively
const { SubscriptProxy } = require('@nejs/subscript-proxy');
// or alternatively
const { SubscriptProxy } = await import('@nejs/subscript-proxy');
// Take an object
let object = { name: 'Cocacola' }
// Choose some proxied key value pairs
SubscriptProxy.applyTo(object, [
['type', 'drink'],
['hasCalories', () => true]
])
// or
SubscriptProxy.applyTo(object, {
type: 'drink',
hasCalories() { return true },
})
// Then use
object.name // 'Cocacola'
object.type // 'drink'
object.hasCalories // true
``
MIT
Contributors are welcome! Please submit a Pull Request.