Search code examples
javascriptes6-proxy

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

11


Solution

  • Trying productCount.value.array, but i will get again proxy - [[target]] - array

    That's not necessarily a problem. If the proxies (apparently there are at least two involved) allow you to access that array and its element 0, and you got the screenshot you showed from console.log(productCount.value), you can do so like this:

    const elementZeroValue = productCount.value.array[0];
    

    Basically, you pretend the proxy isn't there, since it's a facade on the target. (A facade that may well limit or modify what you see.)

    But that's only if the proxies involved allow that access. You can't directly access the [[Target]] of a Proxy, that's part of the point of them.