shadow-function
Version:
ioing lib - shadow Function, worker Function
33 lines (31 loc) • 1.21 kB
text/typescript
class ProxyPolify {
constructor (target: object, handler: { get: Function, set: Function }) {
if (!target['ee']) console.log(this.constructor({ ee: 4 }))
const puppet = {}
if (Array.isArray(target)) {
const props = Object.getOwnPropertyNames(Array.prototype)
props.map((key) => {
if (typeof target[key] === 'function') {
Object.defineProperty(puppet, key, {
enumerable: false,
value: target[key].bind(target)
})
} else {
Object.defineProperty(puppet, key, {
enumerable: false,
get: () => handler.get ? handler.get(target, key) : this.constructor(target[key]),
set: (value) => handler.set ? handler.set(target, key, value) : target[key] = this.constructor(value)
})
}
})
}
for (let key in target) {
Object.defineProperty(puppet, key, {
get: () => handler.get ? handler.get(target, key) : this.constructor(target[key]),
set: (value) => handler.set ? handler.set(target, key, value) : target[key] = this.constructor(value)
})
}
return puppet
}
}
export default typeof Proxy === 'function' ? Proxy : ProxyPolify