Skip to content

Commit f574995

Browse files
committed
proxy()
1 parent 3c9e2f1 commit f574995

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

proxy.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//! The Proxy object enables you to create a proxy for another object, which can intercept and redefine fundamental operations for that object.
2+
3+
const john = { status: "looking for work" };
4+
5+
const handler = {
6+
get: function (target, propName) {
7+
console.log(target);
8+
console.log(propName);
9+
return target[propName];
10+
},
11+
};
12+
13+
// const agent = new Proxy(john, {});
14+
// console.log(agent.status);
15+
16+
const agent = new Proxy(john, handler);
17+
console.log(agent.status);
18+
19+
console.log("---------------------");
20+
21+
const ohi = { job: "student " };
22+
const handler2 = {
23+
set(target, propName, value) {
24+
if (propName === "payRate") {
25+
value = value * 0.85;
26+
}
27+
target[propName] = value;
28+
},
29+
};
30+
const agent2 = new Proxy(ohi, handler2);
31+
agent2.payRate = 1000;
32+
console.log(agent2.payRate);

0 commit comments

Comments
 (0)