Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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've tried many ways. Was googled many blog posts, but found no way to get the target :(

like image 982
Adam Avatar asked Jun 29 '18 07:06

Adam


1 Answers

You can make a copy of the data returned by the proxy using Object.assign():

const target_copy = Object.assign({}, my_proxy); 

This will work for all enumerable own properties existing on the proxy/target.

like image 127
Rashad Saleh Avatar answered Sep 19 '22 09:09

Rashad Saleh