Skip to content

Commit 9c81b18

Browse files
committed
proxy.js
1 parent 20237fe commit 9c81b18

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

proxy.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,26 @@ let target = {
1010
};
1111

1212
let proxy = new Proxy(target, {
13-
get (receiver, name) {
14-
return name in receiver ? receiver[name] : `Howdy, ${name}`
13+
get (obj, prop, val) {
14+
return val in obj ? obj[val] : 'Howdy, ${val}'
15+
},
16+
set (obj, prop, val){
17+
if(prop === 'password'){
18+
if(prop.length<8){
19+
throw new TypeError('The length of password should be greater than 8');
20+
} else {
21+
obj[prop] = val;
22+
}
23+
} else {
24+
obj[prop] = val;
25+
}
1526
}
1627
});
1728

1829
proxy.guest; // "Welcome, Guest"
19-
proxy.userX // "Howdy, UserX"
20-
30+
proxy.userX; // "Howdy, UserX"
31+
proxy.password = 'abc'; //Error The length of password should be greater than 8.
32+
proxy.age = 22; //age=22
2133

2234

2335

0 commit comments

Comments
 (0)