Questions tagged [es6-proxy]

The ES2015 Proxy wraps another object and provides customisable behaviour for basic operations (such as accessing properties). Use cases include providing default values for undefined properties, validating set actions and customising the way objects are iterated.

The Proxy object is used to define custom behavior for fundamental operations (e.g. property lookup, assignment, enumeration, function invocation, etc).

Syntax

var p = new Proxy(target, handler);
243 questions
6
votes
1 answer

How to use a Proxy in javascript to capture a constructor while maintaining the prototype chain?

I want to create a constructor object whose inheritance works as normal, but capture the constructor so I can manipulate the instance object. Using Proxy() almost solves this, but it seems to screw up the inheritance. Note the following example…
Eric Lange
  • 1,755
  • 2
  • 19
  • 25
5
votes
1 answer

How to make JavaScript Proxy's handler methods to be async functions?

Refer the Mozilla document about Proxy() A simple Proxy example: const handler = { get: function(target, prop, receiver) { return Reflect.get(target, prop, receiver); }, set: function(target, prop, receiver) { return…
tigercosmos
  • 345
  • 3
  • 17
5
votes
0 answers

How to create a proxy for HTMLELement

Question How to create a proxy for browser native DOM object? Background I want to intercept the settings for the element style. So I create a proxy for the DOM object. However, it causes error when I use some function like getComputedStyle(). …
Ryths Xia
  • 61
  • 3
5
votes
3 answers

How to check the type of es6 proxy in Javascript?

I am working with ES6 Proxy. I have created a proxy of an array, now when i check the type of proxy it is giving me as Object type. Question: How can i check if the proxy i have created was for array or object? Example: const arr = ['a', 'b',…
Johar Zaman
  • 1,955
  • 17
  • 27
5
votes
1 answer

Proxy ES6 class and maintain prototype chain

I'm proxying methods using the following wrapper: public static wrap(target) { function construct(constructor, args) { const c: any = function(this) { return constructor.apply(this, args); }; c.prototype =…
Tim
  • 7,746
  • 3
  • 49
  • 83
5
votes
0 answers

Unable to use ES6 proxy with window.document object

I have written some code that is supposed to watch for modifications to document.cookie and print to console whenever that happens. var handler = { set: function(target, property, value) { console.log("in proxy"); if…
xrisk
  • 3,790
  • 22
  • 45
5
votes
2 answers

Is it better to use Reflect.defineProperty instead of Object.defineProperty?

Using eslint with React configurations I get an error when using Object.defineProperty. The error says: Avoid using Object.defineProperty, instead use Reflect.defineProperty. (prefer-reflect) On the eslint documentation of prefer-reflect they say…
Daniel Reina
  • 5,764
  • 1
  • 37
  • 50
5
votes
1 answer

await for proxy leads to get of 'then' property, what should I return?

I have a Proxy to mimic a virtual object. Getter of this proxy returns ready values. I have discovered, that if the proxy is awaited, it leads to calling of the 'then' property of my proxy: await myProxy What should my proxy getter return in this…
user2106769
  • 445
  • 5
  • 15
5
votes
2 answers

TypeError with Proxy class - TypeError: 'set' on proxy: trap returned truish for property

I am getting this fun error when using the Proxy class: TypeError: 'set' on proxy: trap returned truish for property 'users' which exists in the proxy target as a non-configurable and non-writable data property with a different value I have a…
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
5
votes
2 answers

Illegal invocation error using ES6 Proxy and node.js

I can not figure out why the following code does not work: var os = new Proxy(require('os'), {}); console.log( os.cpus() ); // TypeError: Illegal invocation whereas var os = require('os'); console.log(Reflect.apply(os.cpus, os, [])); or var os =…
Franck Freiburger
  • 26,310
  • 20
  • 70
  • 95
5
votes
1 answer

What would be a use case for identity-preserving membrane proxies?

When I was reading about ES6 Proxies, it seemed simple enough to understand until I had taken a look at this example. I'm stumped. I don't understand the "wet/dry" terminology that they use, and I have no idea when I'd end up in a case where this…
kpimov
  • 13,632
  • 3
  • 12
  • 18
4
votes
1 answer

Uncaught TypeError: proxy set handler returned false for property '"length"'

I started testing the following proxy pattern, and got the titled error when using the .splice() method. class A extends Array { constructor(...x) { super(...x) return new Proxy(this, { set(o,p,v) { console.log("set:…
hardstuck
  • 43
  • 1
  • 4
4
votes
1 answer

JavaScript proxy return async value on "get"

I have some objects that I am fetching from a web server. Some of the attributes of the objects require additional async parsing, but I want to do that lazily for performance reasons. I am trying to use a proxy to intercept the access and then…
echappy
  • 533
  • 5
  • 13
4
votes
2 answers

How to wrap object being constructed with Proxy inside constructor?

I understand that Proxy can be used to alter object-level behaviors, such as bracket notation get and set. All the examples I can find show constructing an object and then wrapping it with a Proxy call. Is there a way to define a class Foo, using…
Ming
  • 1,613
  • 12
  • 27
4
votes
4 answers

ES6 Proxy: set() trap not triggering, when setting inside target object's method

Example: let foo = {bar: 'baz', method() { this.bar = 'baz2' }} let fooProxy = new Proxy(foo, {set(target, key, val) { console.log('set trap triggered!') }}) fooProxy.bar = 'any value' // as expected: set trap triggered! foo.method() // trap not…
Nurbol Alpysbayev
  • 19,522
  • 3
  • 54
  • 89