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);
console.log(Object.getPrototypeOf(proxy) === Object.prototype);
Set an array as target for the proxy
setTarget(['a', 'b', 'c']);
console.log(proxy[1]);
console.log(Object.getPrototypeOf(proxy) === Array.prototype);
Set a function as target for the proxy
setTarget(() => 5);
console.log(proxy());
console.log(Object.getPrototypeOf(proxy) === Function.prototype);
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());
console.log(Object.getPrototypeOf(proxy));