doc: create a cavet arrow function section

This commit is contained in:
Leon Xu committed 2021-09-09 14:05:03 -07:00
1 parent a279ac0836
commit 043e2255d6
1 file changed
+18
+18
View File
@@ -78,4 +78,22 @@ function draftReply(startingText) {
this.parentNode.appendNode( /*....*/ );
// ...
}
```
### 警告:箭头函数不绑定this关键字
我们已经看到了“this”如何在事件处理程序中引用绑定元素。然而,箭头
函数不会以同样的方式绑定“this”。
```javascript
skittle.addEventListener("dblclick", function() {
// 正确!点击这个=== div.skittle
this.parentNode.removeChild(this);
id("result").textContent = ":O You ate a skittle...";
});
```
```javascript
skittle.addEventListener("dblclick", () => {
// 错误! this===窗口
this.parentNode.removeChild(this);
id("result").textContent = ":O You ate a skittle...";
});
```