This points to the problem. In general, this ends up pointing to the object that called it, 1. In a global scope or normal function, this refers to the global object Window.

console.log(this); function fn() { console.log(this); } /* window. */ fn(); /* window. */ setTimeout(function () { console.log(this); }); The console. The log (" luxuriant line -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ");Copy the code

2. Whoever calls this points to in a method call

var o = { name: "zz", sayHi: function () { console.log(this); // point to the object o},}; o.sayHi(); var btn = document.querySelector("button"); // btn.onclick = function () { // console.log(this); // point to BTN //}; btn.addEventListener("click", function () { console.log(this); // point to BTN});Copy the code

3. Constructor this refers to an instance of the constructor

function Fun() { console.log(this); } var fun = new fun ();Copy the code