-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProxy.js
More file actions
55 lines (50 loc) · 1.33 KB
/
Proxy.js
File metadata and controls
55 lines (50 loc) · 1.33 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
function createProxy(subject) {
//获取本体对象的原型,实现伪继承
const proto = Object.getPrototypeOf(subject);
//代理对象
function Proxy(subject) {
this.subject = subject;
}
Proxy.prototype = Object.create(proto);
//代理方法
Proxy.prototype.hello = function() {
return this.subject.hello() + ' World!';
};
//委托给本体对象
Proxy.prototype.goodbye = function() {
return this.subject.goodbye.apply(this.subject, arguments)
}
return new Proxy(subject)
}
function createProxy2(subject) {
return {
//代理方法
hello: () => {
return subject.hello() + 'World';
},
//委托给本体对象
goodbye: () => {
return subject.goodbye.apply(subject, arguments)
}
}
}
function createProxy3(subject) {
const orihello = subject.hello;
subject.hello = () => {
return orihello.call(this) + 'World'
}
}
const brands = {
car: 'volkswagon',
drink: 'coca cola',
}
const uppercaseBrands = new Proxy(brands, {
get: (target, property) => target[property].toUpperCase()
});
console.log(uppercaseBrands.car, uppercaseBrands.drink);
const evenNumbers = new Proxy([], {
get: (target, index) => index * 2,
has: (target, number) => number%2 === 0
});
console.log(evenNumbers[0],evenNumbers[1],evenNumbers[2]);
console.log(1 in evenNumbers, 2 in evenNumbers);