From 8f43853f81415bbaffe6e97727e23d67dca91b7b Mon Sep 17 00:00:00 2001 From: Leon Xu <6479451-leonxu260@users.noreply.gitlab.com> Date: Thu, 9 Sep 2021 14:08:45 -0700 Subject: [PATCH] doc: create a comparision section --- source/_posts/this关键字.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/source/_posts/this关键字.md b/source/_posts/this关键字.md index cd03876..89c7e7e 100644 --- a/source/_posts/this关键字.md +++ b/source/_posts/this关键字.md @@ -96,4 +96,22 @@ skittle.addEventListener("dblclick", () => { this.parentNode.removeChild(this); id("result").textContent = ":O You ate a skittle..."; }); +``` + +### 用this比较不同的回调函数 +```javascript +function init() { + // this === 窗口 + id("btn1").addEventListener("click", namedFunction)l + id("btn2").addEventListener("click", function() { + console.log("this === " + this); // this === #btn2 + }); + id("btn3").addEventListener("click", () => { + console.log("this === " + this); // this === 窗口 + }); +} + +function namedFunction() { + console.log("this === " + this); // this === #btn1 +} ``` \ No newline at end of file