-
-
Save abozhilov/9eacba362ee293a14a949fffe43e8a3c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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