Proxy

Proxy ( 语言层面做出修改 对编程语言进行编程 meta programming )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 重新定义了obj的get、set方法
const obj = new Proxy(
{},
{
get: function(target, key, receiver) {
console.log(`getting ${key}!`);
return Reflect.get(target, key, receiver);
},
set: function(target, key, value, receiver) {
console.log(`setting ${key}!`);
return Reflect.set(target, key, value, receiver);
}
}
);

obj.count = 1 // setting count!
++ obj.count // getting count! setting count!
1
2
ES6原生提供的Proxy构造函数:
var proxy = new Proxy()
# ES6

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×