doc: add the keyword this section

This commit is contained in:
Leon Xu committed 2021-09-09 13:05:40 -07:00
1 parent 5f51596644
commit 3e7607ae34
1 file changed
+20 -1
+20 -1
View File
@@ -29,4 +29,23 @@ public int compareTo(Object other) {
- 函数调用:alert('Hello World!')
- 方法调用:console.log('Hello World!')
- 构造函数调用:new RegExp ('d')
- 间接调用:alert.call(undefined, 'Hello World!')
- 间接调用:alert.call(undefined, 'Hello World!')
# 关键字this
所有JavaScript代码实际上运行在我们可以用关键字this访问的“一个对象”内部
默认情况下,代码在全局窗口对象中运行(因此this === window)
事件监听器中附加的事件处理程序绑定到元素
在处理程序内部,该元素变成this
```javascript
function init() {
// this === 窗口
id("btn1").addEventListener("click", namedFunction)l
id("btn2").addEventListener("click", function() {
console.log("this === " + this); // this === #btn2
});
}
function namedFunction() {
console.log("this === " + this); // this === #btn1
}
```