-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy.js
More file actions
67 lines (60 loc) · 1.53 KB
/
proxy.js
File metadata and controls
67 lines (60 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// This is simple object.
const someObject = {
prop1: "Property One",
prop2: 1
}
// This is proxy object.
const objectProxy = new Proxy(someObject, {
get (target, prop) {
console.log(`Target is ${target}`);
console.log(`Prop is ${prop}`);
return target[prop];
},
set (target, prop, value) {
if (prop in target) {
target[prop] = value;
} else {
throw new Error("Bad prop.");
}
},
has (target, prop) {
return ["prop1", "prop2"].includes(prop);
},
deleteProperty(target, prop) {
delete target[prop];
return true;
}
});
// This is simple function.
// const someFunc = text => console.log(text);
const someFunc = function(text) {
console.log(text);
}
// This is proxy function.
const funcProxy = new Proxy(someFunc, {
apply(target, thisArg, args) {
console.log(`Target is: ${target}`);
console.log(`This arg is: ${thisArg}`);
console.log(`Args is: ${args}`);
return target.apply(thisArg, args).toUpperCase();
}
});
// This is simple class.
class SomeClass {
constructor(prop1, prop2) {
this.prop1 = prop1;
this.prop2 = prop2;
}
}
// This is proxy class.
const ClassProxy = new Proxy(SomeClass, {
construct(target, args) {
return new Proxy(new target(...args), {
get (t, prop) {
console.log(prop);
return t[prop];
}
})
}
});
const proxy = new ClassProxy("Some property", 123);