doc: create a cavet arrow function section
This commit is contained in:
1 parent
8457e8a775
commit
9613a67f08
1 file changed
+18
@@ -79,3 +79,21 @@ function draftReply(startingText) {
|
|||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 警告:箭头函数不绑定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...";
|
||||||
|
});
|
||||||
|
```
|
||||||
Reference in new issue
Block a user