简单的代理脚本

function watch(obj, name) {
    return new Proxy(obj, {
        get: function(target, property, receiver) {
            try {
                if (typeof target[property] === "function") {
                    console.log("对象 => " + name + ", 读取属性: " + property + ", 值为: function, 类型为: " + typeof target[property]);
                } else {
                    console.log("对象 => " + name + ", 读取属性: " + property + ", 值为: " + target[property] + ", 类型为: " + typeof target[property]);
                }
            } catch (e) {
                // 捕获错误但不处理(保持原代码逻辑)
            }
            return target[property];
        },
        set: function(target, property, newValue, receiver) {
            try {
                console.log("对象 => " + name + ", 设置属性: " + property + ", 值为: " + newValue + ", 类型为: " + typeof newValue);
            } catch (e) {
                // 捕获错误但不处理(保持原代码逻辑)
            }
            return Reflect.set(target, property, newValue, receiver);
        }
    });
}

你可能感兴趣的:(前端,javascript,开发语言)