[Javascript] Intercept property access with Javascript Proxy
2024-09-01 02:21:35
A Javascript Proxy object is a very interesting es6 feature, that allows you to determine behaviors whenever a property is accessed in a target object. In this lesson, you will learn how to use it to prevent users from accessing specific properties in your objects
test("Intercept property access using es6 Proxies", () => {
const obj = {
_id: "klsjdahflkastfaskfdg",
name: "Khaled"
};
const handler = {
get: (target, prop) => {
if (prop[] === "_") {
throw new TypeError("Access denied");
}
return target[prop];
},
set: (target, prop, value) => {
if (prop[] === "_") {
throw new TypeError("Access denied");
}
target[prop] = value;
return true;
}
};
const proxy = new Proxy(obj, handler); expect(() => {
proxy._id;
}).toThrow();
expect(() => {
proxy._id = "something";
}).toThrow();
});
最新文章
- 替换GitBlit的证书为域证书
- Unity 3D 中实现对物体 位置(position) 旋转(rotation) 大小(scale) 的全面控制
- C# 序列化(Serialize)与反序列化(Deserialize)ZZ
- 【POJ】【2104】区间第K大
- HDU 4348 To the moon 可持久化线段树,有时间戳的区间更新,区间求和
- javaweb学习总结十五(web开发的相关概念以及常用服务器介绍)
- BZOJ 2754: [SCOI2012]喵星球上的点名
- SPOJ694 -- DISUBSTR	后缀树组求不相同的子串的个数
- JMeter简单性能测试(适合初学者)
- Windows开机登录认证与Gina DLL
- hibernate的基本配置
- 如何正确使用const、static、extern
- 【一天一道LeetCode】#232. Implement Queue using Stacks
- 巩固java(二)----JVM堆内存结构及垃圾回收机制
- Android 基础一 TextView,Style样式,Activity 传值,选择CheckBox 显示密码
- scroll-view
- Java多线程系列——过期的suspend()挂起、resume()继续执行线程
- SQL服务器模式
- The Beam Model:Stream &; Tables翻译(上)
- 20181009-8 选题 Scrum立会报告+燃尽图 07