/*
Итак, задача следующая. Есть произвольный объект. Если свойство начинается с _ - оно считается закрытым и защищается паролем. Свойство _password точно также защищается паролем и может быть изменено.
*/
var user =
{
name: 'Lex',
age: 28,
job: 'developer',
_password: "access128",
_girlfriend: 'Lipona',
_hobby1: 'chess',
_hobby2: 'violin',
_hobby3: 'bicycle'
}
var user2 =
{
name: 'Lipona',
_age: 30,
_job: 'beutiful interesting girl',
_password: 1526
}
function getPassAccess(object)
{
if ( !('_password' in object) )
{
object._password = '';
}
var pass = object._password;
Object.defineProperty(object, '_password',
{
get()
{
return pass;
},
set(value)
{
pass = value;
}
} );
let objectProxy = new Proxy(object,
{
get(target, prop, receiver)
{
if( !(prop in target) )
throw new ReferenceError("Свойства не существует");
if ( !prop.startsWith('_') )
return Reflect.get(...arguments);
else if( prompt(`введите пароль для доступа к свойству ${prop} объекта ${target.name}`) == pass )
return Reflect.get(...arguments)
else
throw new TypeError("Ошибка доступа");
},
set(target, prop, val, receiver)
{
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.