mutable-proxy
v1.0.0
Published
A library for creating mutable proxies
Downloads
5,545
Readme
MutableProxy
Basic Usage
The factory returns a controller object with functions to affect the mutable state of the proxy
const {
setTarget,
setHandler,
proxy
} = mutableProxyFactory();
Set a simple object as target for the proxy
setTarget({ a: 'apple' });
console.log(proxy.a); // => 'apple'
console.log(Object.getPrototypeOf(proxy) === Object.prototype); // => 'true'
Set an array as target for the proxy
setTarget(['a', 'b', 'c']);
console.log(proxy[1]); // => 'b'
console.log(Object.getPrototypeOf(proxy) === Array.prototype);// => 'true'
Set a function as target for the proxy
setTarget(() => 5);
console.log(proxy()); // => '5'
console.log(Object.getPrototypeOf(proxy) === Function.prototype); // => 'true'
Set an object with a custom prototype for the proxy
class Person {
constructor(name) {
this.name = name;
}
speak() {
return `hi, my name is ${this.name}`;
}
}
setTarget(new Person('John'));
console.log(proxy.speak()); // => 'hi, my name is John'
console.log(Object.getPrototypeOf(proxy)); // => 'Person {}'