function protectObject(object, password) {
if (!password) {
return object;
}
return new Proxy(object, {
get(target, name) {
const pwd = prompt('Enter password');
if (!pwd || pwd !== password) {
throw new Error('Access denied');
}
return target[name];
}
});
};
var a = {a: 1, b: 2, c: 3},
p = protectObject(a, 'asd');
try {
alert(p.a);
} catch (e) {
alert(e.message);
}