From 8457e8a775ffb01dccd23a5b2c0ac77dda635ca6 Mon Sep 17 00:00:00 2001 From: Leon Xu <6479451-leonxu260@users.noreply.gitlab.com> Date: Thu, 9 Sep 2021 13:10:06 -0700 Subject: [PATCH] doc: create what happen to this section --- source/_posts/this关键字.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/source/_posts/this关键字.md b/source/_posts/this关键字.md index e01feac..0c523d2 100644 --- a/source/_posts/this关键字.md +++ b/source/_posts/this关键字.md @@ -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( /*....*/ ); + // ... +} ``` \ No newline at end of file