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