使用 Proxy 类时出现这个有趣的错误:
TypeError: 'set' on proxy: trap returned truish for property 'users' which exists in the proxy target as a non-configurable and non-writable data property with a different value
Run Code Online (Sandbox Code Playgroud)
我有一个以递归方式创建代理对象属性的库,其中任何非原始属性都是代理对象本身,等等:
let mcProxy = function (target) {
const mirrorCache = {};
return new Proxy(target, {
set: function (target, property, value, receiver) {
if (mirrorCache[property]) {
throw new Error(`property ${property} has already been set`);
}
mirrorCache[property] = true;
Object.defineProperty(target, property, {
writable: false,
value: (value && typeof value === 'object') ? …Run Code Online (Sandbox Code Playgroud)