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
2
votes
1 answer

Is there a way to use Javascript ES6 Proxy to spy on Object Methods

Is it possible, given the following object let target = { foo:0, result:[], bar(){ //some code } } to then wrap said object in a Proxy() let handler = { get(){ // code here }, apply(){ // code here } } target = new…
2
votes
1 answer

How to fix `TypeError: Illegal Invocation` on an XMLHttpRequest wrapped in an ES6 Proxy

I'm writing a proxy for XMLHttpRequests, to force them to return a specific status code. I'm able to run functions on proxy, but setting fails with a TypeError: Illegal Invocation. In the example below, this happens on the line oReq.responseType =…
davidsharp
  • 278
  • 2
  • 6
2
votes
0 answers

run cleanups automatically when a block variable gets out of scope in javascript

the answer to this question will probably be "it cannot be done with out writing a custom javaScript parser" but here i go: while developing some lightweight scripting tool i ran into a need to do some automatic cleanup behind the scenes when some…
2
votes
1 answer

how to make Proxy's trap asyncronous?

Let's say we have an object: const obj = { element } and a Proxy with "asyncronous trap": const proxy = new Proxy( obj, { get: async function(obj, prop) { if(prop === 'element') { // wait until child element appears ( -…
Asker
  • 71
  • 2
2
votes
1 answer

Trace method calls with javascript proxy

var handler1 = { get:function(target,key){ if (typeof target[key] == "function"){ var method = Reflect.get(target, key); return method.bind(target); } } } var handler2 = { get:…
Ayush Goel
  • 435
  • 6
  • 16
2
votes
1 answer

Illegal invocation while intercepting method calls with javascript proxy

Can someone explain the following output var handler = { get: function(target,key, receiver){ return new Proxy(Reflect.get(target, key, receiver),handler); }, apply: function(target, thisArg, args){ Reflect.apply(target, thisArg,…
Ayush Goel
  • 435
  • 6
  • 16
2
votes
1 answer

reflection appears to be losing this

I'm trying to build an AOP logger for my classes... I'm having an issue where when i reflect back to the targeted function, the function loses access to this so my AOP kinda looks like this AOP.js class AOP { constructor() { } static…
Jarede
  • 3,310
  • 4
  • 44
  • 68
2
votes
0 answers

ES6 Proxies in Angular Template

I am using RxDB (essentially a wrapper for pouchdb using rxjs), in my Angular application. Each document in the database (called an RxDocument) is a ES6 Proxy. For instance, I could have and RxDocument like so: Object{ arr : getter arr :…
Derek Brown
  • 4,232
  • 4
  • 27
  • 44
2
votes
1 answer

Unexpected Set trap behavior in ES6 Proxy

let ar = []; let p = new Proxy(new Map(), { get: (o, k) => { ar.push(1) return Reflect.get(o, k).bind(o) }, set: (o, k, v) => { ar.push(2) return Reflect.set(o, k, v) } }); p.set(1, 2) p.get(1) console.log(ar) //Outputs [1,1] I am trying…
CoodleNoodle
  • 324
  • 3
  • 17
2
votes
0 answers

How to watch an Array for changes in JS

I understand there used to be Array.observe() which is now deprecated in favor of Proxies. I read the documentation on proxy traps, but I guess I feel it's pretty brittle for arrays... like let's say I have a array of objects called a shopping cart…
james
  • 3,989
  • 8
  • 47
  • 102
2
votes
0 answers

ES6 Proxy calling methods as properties

I have the following class that is utilizing a Proxy for getting properties and methods: class User extends Model { static table = 'users'; _attributes = { id: 1, firstName: 'Testing', lastName: 'Test' }; …
Wolfie
  • 1,369
  • 8
  • 16
2
votes
1 answer

Assign a property above proto proxy

Following code contains proxy with get trap in object's __proto__. When getting some property from the object, according to js logic, trap is called only when object itself doesn't contain corresponding property. So after assignment the property…
Qwertiy
  • 19,681
  • 15
  • 61
  • 128
2
votes
0 answers

ES6 Nested Proxy for Get on Frozen Object

I'm trying to deeply proxy the properties of a frozen object: const a = Object.freeze([{ prop: 1 }]) const proxy = new Proxy(a, { get(target, property) { return new Proxy(target[property], {}); } }) console.log(proxy[0]) This…
Matt Bierner
  • 58,117
  • 21
  • 175
  • 206
2
votes
1 answer

Detect whether a Proxy getter is being called from the Proxy or the target

I'm using JavaScript's Proxy object to create a get() handler to trap property access to an instance of a class. I would like to have different behavior depending on whether the Proxy is being called by methods within the class or by the proxy…
jdelman
  • 683
  • 1
  • 6
  • 20
2
votes
3 answers

Using ES6 Proxy results in any function call "is not a function"

I was trying to be a sneaky developer, and I done got myself lost in ES6 Proxies. Basically I wanted to capture any get or set in a property from another class I wrote and make sure they are stored somewhere else besides the object. It looks like…
Kyle Kinkade
  • 169
  • 1
  • 13