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
95
votes
15 answers

How to test if an object is a Proxy?

I would like to test if a JavaScript object is a Proxy. The trivial approach if (obj instanceof Proxy) ... doesn't work here, nor does traversing the prototype chain for Proxy.prototype, since all relevant operations are effectively backed by the…
GOTO 0
  • 42,323
  • 22
  • 125
  • 158
80
votes
12 answers

How to get the target of a JavaScript Proxy?

How to access the target (which is myArray) of myProxy here? function createProxy() { const myArray = [Math.random(), Math.random()]; return new Proxy(myArray, {}); } const myProxy = createProxy(); I'd like to have a getProxyTarget function…
Adam
  • 4,985
  • 2
  • 29
  • 61
77
votes
5 answers

How to use javascript proxy for nested objects

I have this code in js bin: var validator = { set (target, key, value) { console.log(target); console.log(key); console.log(value); if(isObject(target[key])){ } return true } } var person = { firstName:…
Aflred
  • 4,435
  • 6
  • 30
  • 43
55
votes
7 answers

Can I extend Proxy with an ES2015 class?

I tried to extend Proxy, like so: class ObservableObject extends Proxy {} I used Babel to transpile it to ES5, and I got this error in the browser: app.js:15 Uncaught TypeError: Object prototype may only be an Object or null: undefined I looked…
John L.
  • 1,903
  • 2
  • 13
  • 15
37
votes
3 answers

ES6 Proxy Polyfill for IE11

IE11 does not and will not implement ES2015 Proxy objects. Yet IE11's end of extended support is October 14, 2025. Is there any way to polyfill Proxy objects for IE11? All other browsers support Proxy already. If yes then we would all be able to use…
33
votes
5 answers

Vue Array converted to Proxy object

I'm new to Vue. While making this component I got stuck here. I'm making an AJAX request to an API that returns an array using this code: import axios from 'axios'; export default { data() { return { tickets: [], }; }, methods:…
Eduardo Robles
  • 495
  • 1
  • 5
  • 7
32
votes
9 answers

how do I turn an ES6 Proxy back into a plain object (POJO)?

I'm using a library that turns things into ES6 Proxy objects, and another library that I think is choking because I'm passing it one of these (my code is a travesty, I know), and I couldn't figure out how to unProxy the Proxy object. But I was just…
Sigfried
  • 2,943
  • 3
  • 31
  • 43
29
votes
3 answers

Returning ES6 Proxy from the ES6 class constructor

I want user to only set specific properties to an object but as the same time that object should be constructed from custom class. For example var row = new Row({ name : 'John Doe', email : 'uhiwarale@gmail.com' }, Schema); row can have…
Uday Hiwarale
  • 4,028
  • 6
  • 45
  • 48
29
votes
4 answers

How do I trap arguments to a target method when using a Proxy object?

I'm trying to use Javascript Proxy objects to trap the arguments that are passed to a 'method' of the target that I'm proxying. Please consider this example: var test = { doSomething: function() { console.log( arguments.length ); …
Decent Dabbler
  • 22,532
  • 8
  • 74
  • 106
23
votes
3 answers

Javascript Proxy support in Babel

I'm using babelify version 6.3.0 set to stage 0. ES6 / ES7 are working great. However when I try to use Javascript's proxy functionality: set product(product={}) { this._product = new Proxy({}, {}) } I get: ReferenceError: Can't find variable:…
Allyl Isocyanate
  • 13,306
  • 17
  • 79
  • 130
19
votes
2 answers

What is the difference between Proxy constructor and Reflect?

Is there any significant difference between Reflect and Proxy? From what is documented, it seems that they have pretty much the same capabilities, apart from: Reflect being capable of specifying only one trap at the time. Proxy being…
halfzebra
  • 6,771
  • 4
  • 32
  • 47
18
votes
2 answers

Why does Object.keys() and Object.getOwnPropertyNames() produce different output when called on a Proxy object with ownKeys handler?

I have the following proxy: const p = new Proxy({}, { ownKeys(target) { return ['a', 'b']; }, }); MDN says that: This trap can intercept these…
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
17
votes
3 answers

how to get an array out of a javascript proxy

Hi I was wondering if anyone knew how to get an array out of a proxy's target value in JavaScript. I have something like this : Proxy : [[target]] : Array // the array I need to extract [[handler]] : Object [[IsRevoked]] : false
Jip Helsen
  • 1,034
  • 4
  • 15
  • 30
17
votes
4 answers

Add dynamic values to the console methods at run-time with preservation of original call position and line number intact

I made the following class to 'hijack' the console.log function. The reason behind this is that I want to add and remove values dynamically. It will be used for debug purposes, so the origin of the function call console.log() is important. In the…
17
votes
1 answer

How to Proxy Custom Element (Web Component)

class A extends HTMLElement { constructor() { super() return new Proxy(this, {}) } } window.customElements.define('a-element', A) How can i Proxy custom element? When i try it: Uncaught…
1
2 3
16 17