JS补环境proxy代理使用

ld = {};
ld.config = {};
ld.config.proxy = true;

ld.getType = function getType(obj) {
    return Object.prototype.toString.call(obj);
};

ld.proxy = function proxy(obj, objName) {
    if (!ld.config.proxy) {
        return obj;
    }
    let handle = {
        get: function (target, prop, receiver) {
            console.log("正在获取值");
            let result;
            try {
                result = Reflect.get(target, prop, receiver);
                let type = ld.getType(result);
                if (result instanceof Object) {
                    console.log(
                        `get|obj:【${objName}】-->prop:【${prop.toString()}】,type:【${type}】`
                    );
                    //递归代理
                    result = ld.proxy(result, `${objName}.${prop.toString()}`);
                } else {
                    console.log(
                        `get|obj:【${objName}】-->prop:【${prop.toString()}】,result:【${result}】`
                    );
                }
            } catch (e) {
                console.log(
                    `get|obj:【${objName}】-->prop:【${prop.toString()}】,error:【${e.message
                    }】`
                );
            }
            return result;
        },
        set: function (target, prop, value, receiver) {
            console.log("正在设置值");
            let result;
            try {
                result = Reflect.set(target, prop, value, receiver);
                let type = ld.getType(value);
                if (value instanceof Object) {
                    console.log(
                        `set|obj:【${objName}】-->prop:【${prop.toString()}】,type:【${type}】`
                    );
                } else {
                    console.log(
                        `set|obj:【${objName}】-->prop:【${prop.toString()}】,value:【${value}】`
                    );
                }
            } catch (e) {
                console.log(
                    `set|obj:【${objName}】-->prop:【${prop.toString()}】,error:【${e.message
                    }】`
                );
            }
            return result;
        },
        //通过属性描述符获取属性
        getOwnPropertyDescriptor: function (target, prop) {
            let result; //underfned ,描述符对象
            try {
                result = Reflect.getOwnPropertyDescriptor(target, prop);
                // console.log(result,1111)
                let type = ld.getType(result);
                // console.log(type)
                if (result !== undefined) {
                    console.log(result, 1111);
                    result = ld.proxy(
                        result,
                        `${objName}.${prop.toString()}.PropertyDescriptor`
                    );
                }
                console.log(
                    `getOwnPropertyDescriptor|obj:【${objName}】-->prop:【${prop.toString()}】,type:【${type}】`
                );
            } catch (e) {
                console.log(
                    `getOwnPropertyDescriptor|obj:【${objName}】-->prop:【${prop.toString()}】,error:【${e.message
                    }】`
                );
            }
            return result;
        },
        defineProperty: function (target, prop, descriptor) {
            let result;
            try {
                result = Reflect.defineProperty(target, prop, descriptor);
                console.log(
                    `defineProperty|obj:【${objName}】-->prop:【${prop.toString()}】`
                );
            } catch (e) {
                console.log(
                    `defineProperty|obj:【${objName}】-->prop:【${prop.toString()}】,error:【${e.message
                    }】`
                );
            }
            return result;
        },
        apply: function (target, thisArg, argumentList) {
            console.log("正在函数调用");
            //target:函数对象
            //thisArg:调用函数的this指针
            //argumentList:数组,函数的入参组成的一个列表
            let result;
            try {
                result = Reflect.apply(target, thisArg, argumentList);
                let type = ld.getType(result);
                if (result instanceof Object) {
                    console.log(`apply|function:【${objName}】-->type:【${result}】`);
                } else if (typeof result === "symbol") {
                    console.log(
                        `apply|function:【${objName}】-->result:【${result.toString()}】`
                    );
                } else {
                    console.log(
                        `apply|function:【${objName}】-->【argumentList: ${argumentList}】 -->result:【${result}】`
                    );
                }
            } catch (e) {
                console.log(`apply|function:【${objName}】-->error:【${e.message}】`);
            }
            return result;
        },
        construct: function (target, argArray, newTarget) {
            //target:函数对象
            //argArray:参数列表
            //newTarget:代理对象
            console.log("正在构造函数调用");
            let result;
            try {
                result = Reflect.construct(target, argArray, newTarget);
                console.log(`construct|function:【${objName}】-->type:【${result}】`);
            } catch (e) {
                console.log(
                    `construct|function:【${objName}】-->error:【${e.message}】`
                );
            }
            return result;
        },
    };
    return new Proxy(obj, handle);
};

let symbol = Symbol("123");
user = {
    username: "xm",
    info: {
        name: "小明",
        age: 12,
        add:function(a,b){
            return a+ b
    
        }
    },
    [symbol]: "symbol123",
    
};
user = ld.proxy(user, "user");
// console.log(user.username)
// console.log(user.info.age)
// console.log(user[symbol])
// user.username = {"gpgp":{"ooo":888}}

// console.log(user.username.gpgp.ooo)

console.log(user.info.add(1,2))

// console.log(Object.getOwnPropertyDescriptor(user,"info").value)

// user.name = "小明"

// function add(a, b) {
//     return a + b;
// }

// add = ld.proxy(add, "add");
// // add({"aa":222},"2");

// u = new add();

你可能感兴趣的:(JS补环境proxy代理使用)