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
9
votes
3 answers

Retrieve original target object from existing proxy instance

Say I have a proxy instance like so: const getProxy = function(){ return new Proxy({}, ...); } const proxy = getProxy(); later on, I want to retrieve the target from the proxy, is there some way to do this? something like: const target =…
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
9
votes
3 answers

Array.prototype.forEach() not working when called on a proxy with a get handler

I have the following proxy: const p = new Proxy({ [Symbol.iterator]: Array.prototype.values, forEach: Array.prototype.forEach, }, { get(target, property) { if (property === '0') return 'one'; if (property === '1') return 'two'; if…
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
8
votes
1 answer

How to get deleted or inserted item from proxy array?

I am trying to detect changes in an array of objects using JavaScript proxies. Problem: Any time there is a change in array like deletion or insertion, i want to get that deleted or inserted item. Current Code target = [{ id: 1, a: 'a' }, { id: 2,…
Johar Zaman
  • 1,955
  • 17
  • 27
8
votes
2 answers

Proxy object cannot be added to DOM (traps doesn't trigger either)

I am trying to make a Proxy object of Image to trap properties but even with an empty handler I get an error message. TypeError: Argument 1 of Node.appendChild does not implement interface Node. The proxy object is suppose to act as the target…
bjanes
  • 263
  • 3
  • 11
8
votes
3 answers

Alternatives of JavaScript Proxy

I want to use Proxy on a customized class called ObservableList which contains an Array. Since Proxy is available only after ES6, I wonder if there is any alternative implementation. My requirement is to get updated (rather than get noticed) for…
Ovilia
  • 7,066
  • 12
  • 47
  • 70
8
votes
4 answers

Is there Proxy-object polyfill available google chrome?

Is this even possible? How about other browsers? Any estimates when es6 will be "ready" and rolled out? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy btw. https://github.com/tvcutsem/harmony-reflect Proxy…
pasuna
  • 1,405
  • 2
  • 15
  • 21
7
votes
3 answers

Why isn't ownKeys Proxy trap working with Object.keys()?

In the documentation of the Proxy ownKeys trap on MDN it states that it will intercept Object.keys() calls: This trap can intercept these…
sdgluck
  • 24,894
  • 8
  • 75
  • 90
7
votes
1 answer

Make object or class property only invocable

TL:DR; Is it possible to make a property of object to be invocable ( as a function ) only ? What i mean by this class Foo{ bar(value){ return value } } let newFoo = new Foo() console.log(newFoo.bar(123)) // should work fine as…
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
7
votes
3 answers

Intercept method calls in javascript

What's the equivalent of the __call magic method from PHP ? I was under the impression that Proxy can do this, but it can't. class MyClass{ constructor(){ return new Proxy(this, { apply: function(target, thisArg, args){ …
Alex
  • 66,732
  • 177
  • 439
  • 641
7
votes
2 answers

Transform a Javascript object into a proxy (and not its reference)

I can take a Javascript object o and create a new Proxy object from it: let p = new Proxy(object, { ... }) But is there a way to mutate an existing object reference to track changes on the original object? In particular, is there a way I can track…
yawn
  • 422
  • 1
  • 5
  • 21
7
votes
1 answer

ES6 Proxy set property trap not firing for array length

When working with JavaScript ES6 Proxies, the set property trap for array.length does not fire when assigning array indexes directly. For example: const proxy = new Proxy([], { set: function(obj, name, value) { console.log(`set:…
kgreen
  • 518
  • 6
  • 13
6
votes
1 answer

Using ES6 Proxy to lazily load resources

I am building something like an ActiveRecord class for documents stored in MongoDB (akin to Mongoose). I have two goals: Intercept all property setters on a document using a Proxy, and automatically create an update query to be sent to Mongo. I've…
6
votes
3 answers

undefined returned when using identity-obj-proxy with typescript with jest

I am using jest with typescript in my projects. I am getting undefined for all my .ts files using identity-obj-proxy but .js files work as expected. This is my tsconfig.json: { "compilerOptions": { "target": "es5", …
Abhishek Raj
  • 115
  • 1
  • 1
  • 7
6
votes
1 answer

dispatchEvent(new Proxy(event, {}) does not work

I'm working on an app which uses events. The modules of the app execute in separate containers and I thought about using Proxy to tame the events that are fired. However, I cannot seem to be able to make dispatchEvent accept an event that has been…
user253530
  • 2,583
  • 13
  • 44
  • 61
6
votes
1 answer

lodash cloneDeep remove proxy from object

When I deep clone proxy object then it returns a normal object without proxy. but i want proxy object when i deep clone object. EX: class Abc { constructor() { this.a = 4; return new Proxy(this, { get(target, name) { return…
Deep Patel
  • 301
  • 2
  • 3
  • 10
1 2
3
16 17