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[0] === "_") { throw new TypeError("Access denied"); } return target[prop]; }, set: (target, prop, value) => { if (prop[0] === "_") { 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(); });