Skip to content

Commit 55b7e7b

Browse files
committed
添加Proxy的文档示例,解释其用法及注意事项
1 parent 931cdc6 commit 55b7e7b

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

docs/Web.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,23 @@ button.addEventListener("click", async () => {//使用async关键字
205205

206206
触发事件后便会触发事件侦听器,并触发事件处理函数。但与原生的侦听器的事件处理函数会异步执行不同,dispatchEvent触发的事件处理函数会同步执行,即会阻塞后续代码的执行直到事件处理函数执行完毕。
207207

208+
### Proxy
209+
210+
Proxy用于拦截一些操作,替换成自定义实现。如:
211+
212+
```js
213+
const handler = {
214+
get(target, property){
215+
return name in target ? target[name] : 42;
216+
}
217+
}
218+
const p = new Proxy({}, handler);
219+
p.a = 1;
220+
console.log(p.a,p.b); // 1, 42
221+
```
222+
223+
proxy操作不是随意的,需要保持`不可拓展对象``不可配置属性`的语义不变。比如说,对于不可拓展对象,不能拦截修改使其`proxy``isExtensible`方法返回`true`,否则会抛出`TypeError`异常。
224+
208225
## CSS
209226

210227
### 选择器

0 commit comments

Comments
 (0)