Questions tagged [javascript-proxy]

36 questions
0
votes
1 answer

Typescript: function should return proxy object of generic object type

The following function should create a proxy object with a specific handler, but typescript is throwing a type error when I try it that way. function createProxiedObject(obj: T): T { const handler = { set(obj: {[key:…
0
votes
1 answer

Value of this inside a Proxy trap

I have the following code: function delay(f, ms) { return new Proxy(f, { apply(target, thisArg, args) { console.log(this) console.log(thisArg) setTimeout(() => f.apply(thisArg, args), ms) } …
0
votes
0 answers

JavaScript Error: caught (in promise) TypeError: 'set' on proxy: trap returned falsish for property

I'm attempting to add an "eventlistener" of sorts to trigger when all of the application resources have completed loading. If I understand Proxies correctly, "this is the way" to monitor object property changes. The following code works in my…
TK421
  • 801
  • 5
  • 16
  • 24
0
votes
1 answer

TypeScript types for Proxy with dynamic properties

I want to type a JavaScript Proxy that allows access to arbitrary properties. A contrived example, an object that maps every property to it's own property name: const proxy = new Proxy({}, {   get(target, property) { return prop; …
0
votes
0 answers

React - Use JavaScript Proxy Object to update with Custom Hook

I am seeing a weird behavior with a simple custom React Hook I am trying to come up with. Any ideas on why it's not working as intended would be greatly appreciated. So I have a custom hook called useProxyState. import { useState } from…
0
votes
0 answers

Javascript Proxy Object in React Custom Hook

I have 2 custom hooks useFormInput and other useFormInputWithoutProxies which is using Javascript proxy My Question what benefit does Javascript proxy provides over creating normal custom hooks ? Can Somebody give real world use case or example as…
Sachin
  • 149
  • 1
  • 16
0
votes
1 answer

How to make a web component return a Proxy (and be extended)

The goal is to have a base class A extending HTMLElement that customizes getters and setters. Then class B would extend class A and do stuff. The way to do this is by wrapping class A with a proxy (not the instance, but the class) so B can extend…
0
votes
0 answers

Proxy js object: where are defined arguments when calling a function using Reflect.get()

Trying to understand the internals of Proxy and Reflect built-in objects, I've reached - IMO - an interesting "how does this even work" point: Let's say I want to wrap into a Proxy this object and detect when we're accessing a callable…
Manu Artero
  • 9,238
  • 6
  • 58
  • 73
0
votes
2 answers

Get array in [[target]] from proxy

How can i get this number from productCount.value (screenshot)? Trying productCount.value.array, but i will get again proxy - [[target]] - array Edit 1: If i use productCount.value.array[0] i will get error Invalid value used as weak map key
Jessika
  • 31
  • 8
0
votes
0 answers

In Javascript, is it possible to proxy 'this' within a constructor, without editing the constructor

Imagine I have the following Javascript class: class C { constructor() { this.foo = "bar" } } I have proxied C so I can intercept construction of C: const handler = { construct(target, args, newTarget) { ... } } I need to find a…
ebdavis
  • 105
  • 8
0
votes
1 answer

Can't redirect target of freeze operation via preventExtensions trap

Code: (function () { "use strict"; var proxy; var realTarget; var target; realTarget = {}; target = {}; proxy = new Proxy(target, { isExtensible: function (target) { return…
Melab
  • 2,594
  • 7
  • 30
  • 51
0
votes
1 answer

Extend JavaScript (Vuejs3) proxies with another method

Vue3 uses proxies to wrap object data properties. I have several of these objects where I'd like to add additional functionality to the proxy itself, namely watch get for certain scenarios. Is there a way to extend the proxy object created by Vue,…
Randy Hall
  • 7,716
  • 16
  • 73
  • 151
0
votes
1 answer

Iterating over a Proxy in Vue Composition API

I have a computed value emptyRowsRemoved derived from the Reactivity API. I need to iterate over this value. But because computed() returns a proxy, I am no longer able to iterate over it with a .forEach. I was curious, is the only way to iterate…
Alan
  • 1,067
  • 1
  • 23
  • 37
0
votes
1 answer

How do I intercept sort function within a JS proxy with a function?

My previous related question: How do I intercept sort function within a JS proxy? Given the proxy: function sort(customSort) { /* Write logs */ console.log("Intercepted sort") return this.sort(customSort); } function get( target, prop,…
Blue Granny
  • 772
  • 1
  • 8
  • 24
0
votes
1 answer

Passing Proxy object as thisArgument to apply throws TypeError: Illegal Invocation

I'm trying to trap calls to Storage. As far as I can tell there are two ways to call either setItem or getItem: sessionStorage.setItem("foo", "bar"); let item = sessionStorage.getItem("foo"); …
Mark J Miller
  • 4,751
  • 5
  • 44
  • 74