doc: create what happen to this section

This commit is contained in:
Leon Xu committed 2021-09-09 13:10:06 -07:00
1 parent 3e7607ae34
commit 8457e8a775
1 file changed
+30
+30
View File
@@ -48,4 +48,34 @@ function init() {
function namedFunction() {
console.log("this === " + this); // this === #btn1
}
```
### 更多JavaScript:this关关键字会发生什么
```javascript
let defaultReply = "Hello World";
button.addEventListener("click", function() {
draftReply(defaultReply);
});
```
```javascript
function draftReply(startingText) {
this.parentNode.appendNode( /*....*/);
// ...
}
// this === button
```
vs
```javascript
let defaultReply = "Hello World";
button.addEventListener("click", () => {
draftReply(defaultReply);
});
```
```javascript
function draftReply(startingText) {
this.parentNode.appendNode( /*....*/ );
// ...
}
```