Need private packages and team management tools?Check out npm Teams »

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

Keywords

none

Install

npm i mutable-proxy

DownloadsWeekly Downloads

1,161

Version

1.0.0

License

MIT

Unpacked Size

14.3 kB

Total Files

14

Last publish

Collaborators

  • avatar