mutable-proxy

1.0.0 • Public • Published

MutableProxy

Travis-Ci Code Climate Test Coverage

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 {}'

Dependencies (0)

    Dev Dependencies (15)

    Package Sidebar

    Install

    npm i mutable-proxy

    Weekly Downloads

    3,071

    Version

    1.0.0

    License

    MIT

    Unpacked Size

    14.3 kB

    Total Files

    14

    Last publish

    Collaborators

    • griffingj