Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS Proxy & destructuring assignment

As what i know js Proxies make possible to overload classic object getter with a function call.

So that we can do things like that :

const value = myProxy.value;

All that calling in fact the value getter function inside Proxy.

My question is ... is there a way to use the JS destructuring syntax with JS Proxies ?

So that we could do things like that :

const { value } = myProxy;

Based on my tests, the second way is not working.

like image 903
Poyoman Avatar asked Sep 09 '26 18:09

Poyoman


1 Answers

It is working with a necessary implemented getter.

const
    myProxy = new Proxy({}, {
        get: function(obj, prop) {
            return 42;
        }
    }),
    { value } = myProxy;

console.log(myProxy.value);
console.log(value);
like image 117
Nina Scholz Avatar answered Sep 11 '26 06:09

Nina Scholz