Skip to content

Instantly share code, notes, and snippets.

@abozhilov
Last active May 11, 2021 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abozhilov/9eacba362ee293a14a949fffe43e8a3c to your computer and use it in GitHub Desktop.
Save abozhilov/9eacba362ee293a14a949fffe43e8a3c to your computer and use it in GitHub Desktop.
const create = (() => {
const handler = {
get: (obj, property, reciever) => {
if (!Reflect.has(obj, property)) {
throw new ReferenceError(`Object has no property ${property}`);
}
return Reflect.get(obj, property);
}
};
return (obj) => {
return new Proxy(obj, handler);
};
})();
//Example
const obj = create({
x: 10,
y: 20
});
console.log(obj.x, obj.y); // 10, 20
obj.z = 30;
console.log(obj.z); // 30
console.log(obj.foo); // ReferenceError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment