proxy

作者: 森丶如血 | 来源:发表于2018-04-26 13:57 被阅读0次
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

相关文章

网友评论

      本文标题:proxy

      本文链接:https://www.haomeiwen.com/subject/wdgplftx.html