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
4
votes
2 answers

Adding (recursion?) level awareness to proxy in javascript

I am trying to print out which nodes are being accessed via the getter by overriding my objects getter with a proxy. I am trying to basically test which parts of this large object are not being used by my application. The problem I am having is…
ajmajmajma
  • 13,712
  • 24
  • 79
  • 133
4
votes
2 answers

Execute a function after an ES6 Proxy has completed its sequence of 'gets'?

I have an ES6 Proxy which contains other deeply nested Proxies (generated in the get trap of the root proxy). All of them use the same trap handler. When I try to get the value of a deeply nested object in this collection, the getter is called for…
jonny
  • 3,022
  • 1
  • 17
  • 30
4
votes
1 answer

How to ES6 Proxy an ArrayBuffer or Uint8Array?

These work: crypto.subtle.digest('SHA-512', new Uint8Array([0])) crypto.subtle.digest('SHA-512', new Uint8Array([0]).buffer) These don't: crypto.subtle.digest('SHA-512', new Proxy(new Uint8Array([0]),{})) crypto.subtle.digest('SHA-512', new…
Mihail Malostanidis
  • 2,686
  • 3
  • 22
  • 38
4
votes
2 answers

Is it possible to reference a Proxy from its own handler object?

I have a need to reference the current Proxy instance from inside its own handler. I haven't seen this mentioned in any of the documentation I've read and I'm just curious if there's any natural way to do this. The thing is, inside the handler…
devios1
  • 36,899
  • 45
  • 162
  • 260
4
votes
1 answer

Detect when the reference of a variable changes

I am currently writing a tool that monitors changes made to an object using a Proxy. So I have a function watchObject that takes an object as the argument and wraps it inside a Proxy where the handlers corresponding to changes in the object call…
Seddiki Anass
  • 306
  • 2
  • 19
4
votes
2 answers

Javascript Proxy set() local property on inherited objects

According to MDN, handler.set() can trap Inherited property assignment: Object.create(proxy)[foo] = bar; In which case, how does one both monitor and allow local assignments on inherited objects? var base = { foo: function(){ return…
blackening
  • 903
  • 6
  • 14
4
votes
1 answer

Proxying a recursive function

Imagine a simple recursive function, which we are trying to wrap in order to instrument input and output. // A simple recursive function. const count = n => n && 1 + count(n-1); // Wrap a function in a proxy to instrument input and…
user663031
4
votes
2 answers

Use ES6 proxy to trap Object.hasOwnProperty

I want to use an ES6 proxy to trap the following common code: for (let key in trapped) { if (!Object.prototype.hasOwnProperty.call(obj, key)) continue; let value = trapped[key]; //various code } But after reviewing the proxy…
GregRos
  • 8,667
  • 3
  • 37
  • 63
4
votes
1 answer

Proxy index gets converted to string

Trying out the new Proxy objects, I am surprised that when a proxy is set the key is automatically converted to a string: var arr = ['a', 'b', 'c']; arr = new Proxy(arr, { get: (original, key) => { alert(typeof key); return…
Francisco Presencia
  • 8,732
  • 6
  • 46
  • 90
4
votes
1 answer

JS Proxying HTML5 canvas context

I'm hoping to proxy the canvas API so I can test that abstracted methods do actually draw to the canvas, however I'm hitting issues where after proxing I get an error: 'strokeStyle' setter called on an object that does not implement interface…
thelastshadow
  • 3,406
  • 3
  • 33
  • 36
4
votes
2 answers

Using ES6 Proxies to hide private properties

I'm trying to create a function that hides private properties on Objects as well as possible. I would define private properties here as those that begin with an underscore, eg. _password. Below is what I've got so far (thanks to Nicolas Bevacqua's…
nils
  • 25,734
  • 5
  • 70
  • 79
3
votes
1 answer

Why does this proxy handler cause infinite recursion?

I built a hook for the toString method using ES6 Proxy. While debugging some issues I noticed the console.log of handler getting called for no reason. class Hook { constructor(object) { this.object = object; } …
jeffbRTC
  • 1,941
  • 10
  • 29
3
votes
2 answers

How to detect the end of a get sequence in a proxy object?

In order to avoid error when accessing deeply nested properties, I wrote a proxy-returning function: const safe_access = obj => new Proxy(obj, { get: (o, k) => o[k] == null ? safe_access({}) : typeof o[k] === 'object' …
customcommander
  • 17,580
  • 5
  • 58
  • 84
3
votes
1 answer

Why is [[GetPrototypeOf]] an Invariant of a Javascript Proxy?

One application for a Javascript Proxy object is to reduce network traffic by sending data over the wire as an array of arrays, along with an object listing the field names and index of each field (ie. a field map). (instead of an array of objects…
Casey
  • 490
  • 7
  • 11
3
votes
1 answer

Determine if a function is accessed or invoked in the proxy handler

I have a proxy handler like this: let handler = { get: function(target, name) { let member = target[name]; if (typeof member === 'function') { return function() { // …
Tanmay
  • 3,009
  • 9
  • 53
  • 83