322 questions
104
votes
15
answers
43k
views
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 ...
93
votes
13
answers
93k
views
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()...
81
votes
5
answers
36k
views
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 ...
67
votes
7
answers
22k
views
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 ...
44
votes
9
answers
56k
views
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 ...
37
votes
3
answers
41k
views
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 ...
35
votes
5
answers
68k
views
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() {
...
30
votes
4
answers
11k
views
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() ...
29
votes
2
answers
21k
views
How can I modify a constructor so that it return a proxy that prevents adding new properties to the object?
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 : '...
29
votes
3
answers
37k
views
What does Proxy mean in the console in Vue 3?
I'm shuffling an array and getting a weird message in the console.
My JSON file looks like this:
[
{
"id": 1,
"name": "Sushi",
"image": "...
28
votes
3
answers
11k
views
How to control property enumeration (for...in) with Proxy objects?
I'm wrapping an object in a Proxy and then iterate through it. How can I control the keys it iterates through?
The proxy works if I don't override the keys:
var obj = {"hello": "world"}
var proxy =...
25
votes
3
answers
65k
views
how to get an array out of a javascript proxy
I was wondering 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
[[...
23
votes
3
answers
15k
views
Can Babel transpile code using Proxy into ES5?
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({}, {})...
23
votes
5
answers
6k
views
Major use cases for ES6 proxies
I recently got to know about ES6 proxies but I don't see a good reason to use it. I mean, everything that one could do with Proxy can be done without it, except if I'm missing something.
For example,...
20
votes
2
answers
6k
views
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 ...
19
votes
2
answers
4k
views
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 operations:
Object.getOwnPropertyNames(...
18
votes
2
answers
11k
views
Why is Proxy to a Map object in ES2015 not working
I'm running the following script through Google Chrome Version 57.0.2987.133:
var loggingProxyHandler = {
"get" : function(targetObj, propName, receiverProxy) {
let ret = Reflect....
17
votes
3
answers
11k
views
How to get proxy's handler from proxy object?
For example, if I have this handler/proxy (from the MDN example)...
var handler = {
get: function(target, name){
return name in target?
target[name] :
37;
}
};
...
17
votes
1
answer
4k
views
JavaScript - Proxy set vs. defineProperty
I want to build a proxy that detects changes to an object:
New properties are defined.
Existing properties are changed.
Code Sample 1 - defineProperty
const me = {
name: "Matt"
}
const ...
17
votes
4
answers
3k
views
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 ...
17
votes
1
answer
6k
views
Properly building Javascript proxy set handlers for arrays
What is the correct way to build Javascript proxies for arrays so that 'set' handlers do not get invoked multiple times for a single change to the array?
Here is what I mean:
I want to wrap a simple ...
17
votes
1
answer
3k
views
How to Proxy Custom Element (Web Component)
class A extends HTMLElement {
constructor() {
super()
return new Proxy(this, {})
}
}
window.customElements.define('a-element', A)
<a-element></a-element>
How can ...
14
votes
1
answer
2k
views
Why is Set Incompatible with Proxy?
JavaScript Set appears to be entirely incompatible with JavaScript proxies, attempting to Proxy() a Set()
var p = new Proxy(new Set(), {
add(target, val, receiver) {
console.log('in add: ', ...
12
votes
3
answers
1k
views
Custom Array-like getter in JavaScript
I have a simple ES6 class, like so:
class Ring extends Array {
insert (item, index) {
this.splice(index, 0, item);
return this;
}
}
I want to make it so that the indexing for ...
12
votes
3
answers
5k
views
ES6 proxied class, access private property (Cannot read private member #hidden from an object whose class did not declare it)
Im playing around with proxy objects, classess and private properties.
And came across this error message:
/home/marc/projects/playground/pipeline/clsss.js:14
this.#hidden = !this.#hidden;
...
11
votes
3
answers
13k
views
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 ...
11
votes
2
answers
12k
views
Typescript Compiler Does Not Know About ES6 Proxy Trap on Class
I have an abstract class:
abstract class Foo {
abstract bar(): string;
}
I have some classes that extend Foo:
class Foo1 extends Foo {
bar(): string { return 'foo1'; }
}
class Foo2 extends ...
11
votes
2
answers
5k
views
Function Proxy .toString() Errors
I am trying to call .toString() on a function proxy.
Simply creating a function proxy and calling toString causes "TypeError: Function.prototype.toString is not generic", setting the toString to ...
10
votes
3
answers
4k
views
Using Proxy.apply() on Node.js does not work. Is this a bug or am I doing it wrong?
I am using Proxy to Proxy an object. The getter and setter work fine like expected. However, the apply method is never called.
var p = new Proxy({}, {
/* getter */
get(target, name) {
...
10
votes
3
answers
6k
views
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'...
10
votes
1
answer
4k
views
create structured clone of Proxy
I have a class that returns a Proxy from the constructor. When I try to store instances of this class in IndexedDB, or send the object using window.postMessage(), I receive an error stating that the ...
9
votes
3
answers
2k
views
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 operations:
Object.getOwnPropertyNames()
Object....
9
votes
1
answer
1k
views
Why does 'await' trigger '.then()' on a Proxy returned by an 'async' function ?
I am compiling the code with babel (env), compiling down to ES5.
Here's the code:
(async () => {
const p = async () => {
return new Proxy({}, {
get: (...
9
votes
1
answer
305
views
Is using `with` statement with Proxies a bad practice?
First of all, I want to clarify, I know that with is deprecated, and using it is generally a bad practice.
However, my question is about a special case: using a special Proxy object as the parameter ...
8
votes
7
answers
3k
views
Has anyone used ECMAScript Proxies yet? [closed]
I am trying to get a hang of ESx (Harmony?) Proxies. I think I know the basics now, but I don't think I'm capable of taking advantage of them.
Has anyone managed to use them for any good? I don't ...
8
votes
3
answers
13k
views
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": {...
8
votes
2
answers
3k
views
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 ...
8
votes
4
answers
3k
views
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. ...
8
votes
3
answers
3k
views
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 ...
8
votes
2
answers
2k
views
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.
...
7
votes
3
answers
6k
views
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: ...
7
votes
2
answers
5k
views
Is it possible to Proxy primitives (strings, numbers)?
I'm exploring Proxies in JavaScript, and I want to know if there are any ways to Proxy primitives. If I try to do so:
new Proxy('I am a string');
It throws Uncaught TypeError: `target` argument of ...
7
votes
3
answers
3k
views
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 ...
7
votes
2
answers
3k
views
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 ...
7
votes
1
answer
187
views
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()
...
7
votes
1
answer
3k
views
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(...
6
votes
1
answer
2k
views
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 ...
6
votes
2
answers
4k
views
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....
6
votes
3
answers
5k
views
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 ...
6
votes
3
answers
12k
views
'get' on proxy: property 'items' is a read-only and non-configurable data property on the proxy target but the proxy did not return its actual value
What does this JavaScript error actually mean? I'm not asking about my specific case, just generally how is this error message meant to be understood?
TypeError: 'get' on proxy: property 'items' is a ...