JavaScript Proxy在实际工作中的一些应用

25,658次阅读
没有评论

共计 1765 个字符,预计需要花费 5 分钟才能阅读完成。

前言

Proxy,我之前文章中有写过,今天简单介绍几个工作中可以使用的案例。希望对大家有帮助,需要的时候可以使用,但是不提倡乱用。

Proxy 处理器 get 和 set 的介绍

//movie is a target
const movie = {
    name: "Pulp Fiction",
    director: "Quentin Tarantino"
};

//this is a handler
const handler = {
    //get is a trap
    get: (target, prop) => {if (prop === 'director') {return 'God'}
        return target[prop]
    },

    set: function (target, prop, value) {if (prop === 'actor') {target[prop] = 'John Travolta'
        } else {target[prop] = value
        }
    }
};

const movieProxy = new Proxy(movie, handler);

console.log(movieProxy.director); //God

movieProxy.actor = "Tim Roth";
movieProxy.actress = "Uma Thurman";

console.log(movieProxy.actor); //John Travolta
console.log(movieProxy.actress); //Uma Thurman

应用一 验证属性赋值

const handler = {set: function (target, prop, value) {const houses = ['Stark', 'Lannister'];
        if (prop === 'house' && !(houses.includes(value))) {throw new Error(`House ${value} does not belong to allowed ${houses}`)
        }
        target[prop] = value
    }
};

const gotCharacter = new Proxy({}, handler);

gotCharacter.name = "Jamie";
gotCharacter.house = "Lannister";

console.log(gotCharacter);

gotCharacter.name = "Oberyn";
gotCharacter.house = "Martell";

应用二 根据定义和状态实现功能,例如发送邮件

const sendEmail = () => {console.log("haorooms test status 是 complete 的发邮件")
};


const handler = {set: function (target, prop, value) {if (prop === 'status' && value === 'complete') {sendEmail()
        }
        target[prop] = value
    }
};

const tasks = new Proxy({}, handler);

tasks.status = "complete";

应用三 设置数据缓存和超时

const cacheTarget = (target, ttl = 60) => {const CREATED_AT = Date.now();
    const isExpired = () => (Date.now() - CREATED_AT) > (ttl * 1000);
    const handler = {get: (target, prop) => isExpired() ? undefined : target[prop]
    };
    return new Proxy(target, handler)
};

const cache = cacheTarget({age: 25}, 5);

console.log(cache.age);

setTimeout(() => {console.log(cache.age)
}, 6 * 1000);

结果:

25
undefined

也就是在 5 秒之内可以返回,超时不返回。

缺点

虽然 Proxy 具备一些很神奇的功能, 但在使用时仍然具有一些不得不小心应对的限制:

性能会受到显著的影响. 在注重性能的代码中应该避免对 Proxy 的使用

没有办法区分判断一个对象是一个 Proxy 的对象或者是 target 的对象

Proxy 可能会导致代码在可读性上面出现问题

    正文完
     0
    Yojack
    版权声明:本篇文章由 Yojack 于1970-01-01发表,共计1765字。
    转载说明:
    1 本网站名称:优杰开发笔记
    2 本站永久网址:https://yojack.cn
    3 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行删除处理。
    4 本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
    5 本站所有内容均可转载及分享, 但请注明出处
    6 我们始终尊重原创作者的版权,所有文章在发布时,均尽可能注明出处与作者。
    7 站长邮箱:laylwenl@gmail.com
    评论(没有评论)