proxy

function add (a, b) {
    this.a = a;
    this.b = b;
}

// 实列取不到值,只有函数.属性
add.c = 10;
add.sql = '';
// 实列能取到,函数.属性取不到
add.prototype.d = 10;

const proxy_add = new Proxy(add, {
    construct: function (target, args) {
        return new target(...args);
    },
    // target是构造时的对象, property是.某个属性时的属性名(如: proxy.c --> property = c)
    get: function (target, property) {
        if (property === 'sql') {
            return target[property];
        }
        target.sql += property + ' ';
        return proxy_add;
    },
});
let cc = new proxy_add(1, 2, 3);

console.log(cc.a + cc.b + cc.d);
console.log(proxy_add.select.username.from.users.where.user_id.equal.sql);

// 13
// select username from users where user_id equal
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容