如何获取JavaScript代理的目标? [英] How to get the target of a JavaScript Proxy?

查看:73
本文介绍了如何获取JavaScript代理的目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function createProxy() {
    const myArray = [Math.random(), Math.random()];
    return new Proxy(myArray, {});
}

const myProxy = createProxy();

如何在此处访问myProxytarget(即myArray)?

How to access the target (which is myArray) of myProxy here?

我尝试了很多方法.在Google上搜索了许多博客文章,但没有找到达到目标的方法:(

I've tried many ways. Was googled many blog posts, but found no way to get the target :(

推荐答案

您可以使用

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.

这篇关于如何获取JavaScript代理的目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆